/
RestClientError.ts
42 lines (33 loc) · 1.11 KB
/
RestClientError.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
import { UploadcareError } from '@uploadcare/api-client-utils'
export type RestClientErrorOptions = {
request?: Request
response?: Response
}
const DEFAULT_MESSAGE = 'Unknown error'
/**
* TODO: it's better to split errors into something like Runtime error and
* ServerError (RestApiError)
*/
export class RestClientError extends UploadcareError {
readonly status?: number
readonly statusText?: string
readonly request?: Request
readonly response?: Response
constructor(message?: string | null, options: RestClientErrorOptions = {}) {
super()
this.name = 'RestClientError'
this.request = options.request
this.response = options.response
this.status = options.response?.status
this.statusText = options.response?.statusText
const msg = message ?? this.statusText ?? DEFAULT_MESSAGE
const status =
this.status || this.statusText
? `[${[this.status, msg === this.statusText ? '' : this.statusText]
.filter(Boolean)
.join(' ')}] `
: ''
this.message = status + msg
Object.setPrototypeOf(this, RestClientError.prototype)
}
}