|
1 | 1 | export class RedactableError extends Error { |
2 | 2 | constructor( |
3 | | - cause: Error | undefined, |
| 3 | + cause: ErrorLike | undefined, |
4 | 4 | private readonly strings: TemplateStringsArray, |
5 | 5 | private readonly values: unknown[], |
6 | 6 | ) { |
@@ -54,19 +54,35 @@ export function redactableError( |
54 | 54 | ...values: unknown[] |
55 | 55 | ): RedactableError; |
56 | 56 | export function redactableError( |
57 | | - error: Error, |
| 57 | + error: ErrorLike, |
58 | 58 | ): (strings: TemplateStringsArray, ...values: unknown[]) => RedactableError; |
59 | 59 |
|
60 | 60 | export function redactableError( |
61 | | - errorOrStrings: Error | TemplateStringsArray, |
| 61 | + errorOrStrings: ErrorLike | TemplateStringsArray, |
62 | 62 | ...values: unknown[] |
63 | 63 | ): |
64 | 64 | | ((strings: TemplateStringsArray, ...values: unknown[]) => RedactableError) |
65 | 65 | | RedactableError { |
66 | | - if (errorOrStrings instanceof Error) { |
| 66 | + if (isErrorLike(errorOrStrings)) { |
67 | 67 | return (strings: TemplateStringsArray, ...values: unknown[]) => |
68 | 68 | new RedactableError(errorOrStrings, strings, values); |
69 | 69 | } else { |
70 | 70 | return new RedactableError(undefined, errorOrStrings, values); |
71 | 71 | } |
72 | 72 | } |
| 73 | + |
| 74 | +export interface ErrorLike { |
| 75 | + message: string; |
| 76 | + stack?: string; |
| 77 | +} |
| 78 | + |
| 79 | +function isErrorLike(error: any): error is ErrorLike { |
| 80 | + if ( |
| 81 | + error.message !== undefined && |
| 82 | + typeof error.message === "string" && |
| 83 | + (error.stack === undefined || typeof error.stack === "string") |
| 84 | + ) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + return false; |
| 88 | +} |
0 commit comments