Add an error utility for messages and status codes

This commit is contained in:
Josh Gross
2023-03-15 17:36:51 -04:00
parent 7bb8ff9aae
commit 8279bc1697
3 changed files with 59 additions and 1 deletions
+27
View File
@@ -0,0 +1,27 @@
import {RequestError} from "@octokit/types";
export function errorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
if (typeof error === "string") {
return error;
}
if ("name" in (error as RequestError)) {
return (error as RequestError).name;
}
const status = errorStatus(error);
if (status) {
return `HTTP ${status}`;
}
return "Unknown error";
}
export function errorStatus(error: unknown): number | undefined {
if ("status" in (error as RequestError)) {
return (error as RequestError).status;
}
}