Add an error utility for messages and status codes
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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