-
Notifications
You must be signed in to change notification settings - Fork 16
/
VoucherifyError.ts
44 lines (37 loc) · 1.28 KB
/
VoucherifyError.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { AxiosError } from 'axios'
/**
* @internal
*/
export class VoucherifyError extends Error {
public code: number
public key: string
public details?: string
public request_id?: string
public resource_id?: string
public resource_type?: string
public related_object_ids?: string[]
public related_object_type?: string
public related_object_total?: number
public error?: { message: string }
public cause?: AxiosError
constructor(statusCode: number, body?: unknown, axiosError?: AxiosError) {
body = body ?? {}
const message = (<any>body)?.message || generateMessage(body, statusCode)
super(message)
this.code = (<any>body).code
this.key = (<any>body).key
this.details = (<any>body).details
this.request_id = (<any>body).request_id
this.resource_id = (<any>body).resource_id
this.resource_type = (<any>body).resource_type
this.related_object_ids = (<any>body).related_object_ids
this.related_object_type = (<any>body).related_object_type
this.related_object_total = (<any>body).related_object_total
this.error = (<any>body).error
this.cause = axiosError
}
}
function generateMessage(body: unknown, statusCode: number) {
body = typeof body === 'string' ? body : JSON.stringify(body, null, 2)
return `Unexpected status code: ${statusCode} - Details: ${body}`
}