Add an error utility for messages and status codes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user