| 1 | import type {Headers} from './../../types/index'; |
| 2 | |
| 3 | export class APIError extends Error { |
| 4 | readonly status: number | undefined; |
| 5 | readonly headers: Headers | undefined; |
| 6 | readonly error: Object | undefined; |
| 7 | |
| 8 | readonly code: string | null | undefined; |
| 9 | readonly param: string | null | undefined; |
| 10 | readonly type: string | undefined; |
| 11 | |
| 12 | readonly request_id: string | null | undefined; |
| 13 | |
| 14 | constructor( |
| 15 | status: number | undefined, |
| 16 | error: Object | undefined, |
| 17 | message: string | undefined, |
| 18 | headers: Headers | undefined, |
| 19 | ) { |
| 20 | super(APIError.makeMessage(status, error, message)); |
| 21 | this.status = status; |
| 22 | this.headers = headers; |
| 23 | this.request_id = headers?.['lb-request-id']; |
| 24 | |
| 25 | const data = error as Record<string, any>; |
| 26 | this.error = data; |
| 27 | this.code = data?.['code']; |
| 28 | this.status = data?.['status']; |
| 29 | // this.param = data?.['param']; |
| 30 | // this.type = data?.['type']; |
| 31 | } |
| 32 | |
| 33 | private static makeMessage( |
| 34 | status: number | undefined, |
| 35 | error: any, |
| 36 | message: string | undefined, |
| 37 | ): string { |
| 38 | const msg = error?.message |
| 39 | ? typeof error.message === 'string' |
| 40 | ? error.message |
| 41 | : JSON.stringify(error.message) |
| 42 | : error |
| 43 | ? JSON.stringify(error) |
| 44 | : message; |
| 45 | |
| 46 | if (status && msg) { |
| 47 | return `${status} ${msg}`; |
| 48 | } |
| 49 | if (status) { |
| 50 | return `${status} status code (no body)`; |
| 51 | } |
| 52 | if (msg) { |
| 53 | return msg; |
| 54 | } |
| 55 | return '(no status code or body)'; |
| 56 | } |
| 57 | |
| 58 | static generate( |
| 59 | status: number | undefined, |
| 60 | errorResponse: Object | undefined, |
nothing calls this directly
no outgoing calls
no test coverage detected