| 1 | import stringify from "json-stable-stringify"; |
| 2 | |
| 3 | export class SerializableError<T> extends Error { |
| 4 | public code: number; |
| 5 | |
| 6 | public data: T; |
| 7 | |
| 8 | constructor({ code, message, data }: { code: number; message: string; data?: T }) { |
| 9 | if (!Number.isInteger(code)) { |
| 10 | throw new Error("code must be an integer"); |
| 11 | } |
| 12 | if (!message || typeof message !== "string") { |
| 13 | throw new Error("message must be string"); |
| 14 | } |
| 15 | |
| 16 | super(message); |
| 17 | this.code = code; |
| 18 | if (data !== undefined) { |
| 19 | this.data = data; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | toString(): string { |
| 24 | return stringify({ |
| 25 | code: this.code, |
| 26 | message: this.message, |
| 27 | data: this.data, |
| 28 | stack: this.stack, |
| 29 | }); |
| 30 | } |
| 31 | } |
nothing calls this directly
no outgoing calls
no test coverage detected