(error: unknown)
| 95 | } |
| 96 | |
| 97 | export function errorFormat(error: unknown): string { |
| 98 | if (error instanceof Error) { |
| 99 | return error.stack ?? `${error.name}: ${error.message}` |
| 100 | } |
| 101 | |
| 102 | if (typeof error === "object" && error !== null) { |
| 103 | try { |
| 104 | const json = JSON.stringify(error, null, 2) |
| 105 | // Plain objects whose own properties are all non-enumerable (or empty) |
| 106 | // serialize to "{}", which prints as a useless bare `{}` on stderr. |
| 107 | // Fall back to a custom toString first, then to ctor name + own prop names. |
| 108 | if (json === "{}") { |
| 109 | const str = String(error) |
| 110 | if (str && str !== "[object Object]") return str |
| 111 | const ctor = error.constructor?.name |
| 112 | const prefix = ctor && ctor !== "Object" ? ctor : "Error" |
| 113 | const names = Object.getOwnPropertyNames(error) |
| 114 | return names.length === 0 ? `${prefix} (no message)` : `${prefix} { ${names.join(", ")} }` |
| 115 | } |
| 116 | return json |
| 117 | } catch { |
| 118 | return "Unexpected error (unserializable)" |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return String(error) |
| 123 | } |
| 124 | |
| 125 | export function errorMessage(error: unknown): string { |
| 126 | if (error instanceof Error) { |
no outgoing calls
no test coverage detected