File size: 1,454 Bytes
21dd449 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import type { JsonObject } from "./vendor/type-fest/basic";
export async function createApiError(
response: Response,
opts?: { requestId?: string; message?: string }
): Promise<never> {
const error = new HubApiError(response.url, response.status, response.headers.get("X-Request-Id") ?? opts?.requestId);
error.message = `Api error with status ${error.statusCode}${opts?.message ? `. ${opts.message}` : ""}`;
const trailer = [`URL: ${error.url}`, error.requestId ? `Request ID: ${error.requestId}` : undefined]
.filter(Boolean)
.join(". ");
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
const json = await response.json();
error.message = json.error || json.message || error.message;
if (json.error_description) {
error.message = error.message ? error.message + `: ${json.error_description}` : json.error_description;
}
error.data = json;
} else {
error.data = { message: await response.text() };
}
error.message += `. ${trailer}`;
throw error;
}
/**
* Error thrown when an API call to the Hugging Face Hub fails.
*/
export class HubApiError extends Error {
statusCode: number;
url: string;
requestId?: string;
data?: JsonObject;
constructor(url: string, statusCode: number, requestId?: string, message?: string) {
super(message);
this.statusCode = statusCode;
this.requestId = requestId;
this.url = url;
}
}
export class InvalidApiResponseFormatError extends Error {}
|