* Safely extract an error message from an unknown error value. * Handles Error instances, objects with message properties, and other values. * * When the extracted message looks like an HTML page (e.g. GitHub's "Unicorn" * 504 error page), it is replaced with a concise human-readable description
(error)
| 24 | * @returns {string} The error message as a string |
| 25 | */ |
| 26 | function getErrorMessage(error) { |
| 27 | // prettier-ignore |
| 28 | const errorAsAny = /** @type {any} */ (error); |
| 29 | let message; |
| 30 | if (error instanceof Error) { |
| 31 | message = error.message; |
| 32 | } else if (error && typeof error === "object" && "message" in error && typeof error.message === "string") { |
| 33 | message = error.message; |
| 34 | } else { |
| 35 | message = String(error); |
| 36 | } |
| 37 | |
| 38 | if (isHtmlContent(message)) { |
| 39 | const status = errorAsAny != null && typeof errorAsAny.status === "number" ? errorAsAny.status : null; |
| 40 | return status != null ? `GitHub returned an unexpected HTML response (HTTP ${status})` : "GitHub returned an unexpected HTML response"; |
| 41 | } |
| 42 | |
| 43 | return message; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Check if an error is due to a locked issue/PR/discussion. |
no test coverage detected