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
+2 -1
View File
@@ -43,6 +43,7 @@
"@github/actions-languageservice": "^0.1.167",
"@github/actions-workflow-parser": "^0.1.167",
"@octokit/rest": "^19.0.7",
"@octokit/types": "^9.0.0",
"vscode-languageserver": "^8.0.2",
"vscode-languageserver-textdocument": "^1.0.7",
"yaml": "^2.1.3"
@@ -62,4 +63,4 @@
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
}
}
}
+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;
}
}