2023-05-07 00:46:37 -03:00

25 lines
526 B
TypeScript

export interface GithubError extends Error {
url: string;
status: number;
headers: Response["headers"];
}
function getErrorText(res: Response) {
try {
return res.text();
} catch (err) {
return res.statusText;
}
}
export async function getError(msg: string, res: Response) {
const errorText = await getErrorText(res);
const error = new Error(`${msg} (${res.status}): ${errorText}`) as GithubError;
error.url = res.url;
error.status = res.status;
error.headers = res.headers;
return error;
}