(code: number, message: string, data?: Data)
| 27 | public data?: Data; |
| 28 | |
| 29 | constructor(code: number, message: string, data?: Data) { |
| 30 | if (!Number.isInteger(code)) { |
| 31 | throw new Error('"code" must be an integer.'); |
| 32 | } |
| 33 | |
| 34 | if (!message || typeof message !== 'string') { |
| 35 | throw new Error('"message" must be a non-empty string.'); |
| 36 | } |
| 37 | |
| 38 | if (dataHasCause(data)) { |
| 39 | // @ts-expect-error - Error class does accept options argument depending on runtime, but types are mapping to oldest supported |
| 40 | super(message, { cause: data.cause }); |
| 41 | |
| 42 | // Browser backwards-compatibility fallback |
| 43 | if (!hasProperty(this, 'cause')) { |
| 44 | Object.assign(this, { cause: data.cause }); |
| 45 | } |
| 46 | } else { |
| 47 | super(message); |
| 48 | } |
| 49 | |
| 50 | if (data !== undefined) { |
| 51 | this.data = data; |
| 52 | } |
| 53 | |
| 54 | this.code = code; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the error as JSON-serializable object. |
nothing calls this directly
no test coverage detected