-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathApiError.ts
44 lines (33 loc) · 1.21 KB
/
ApiError.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 { HTTPError, RequestError } from 'got'
export interface TransloaditErrorResponseBody {
error?: string
message?: string
assembly_ssl_url?: string
assembly_id?: string
}
export class ApiError extends Error {
override name = 'ApiError'
// there might not be an error code (or message) if the server didn't respond with any JSON response at all
// e.g. if there was a 500 in the HTTP reverse proxy
code?: string
rawMessage?: string
assemblySslUrl?: string
assemblyId?: string
override cause?: RequestError | undefined
constructor(params: { cause?: RequestError; body: TransloaditErrorResponseBody | undefined }) {
const { cause, body = {} } = params
const parts = ['API error']
if (cause instanceof HTTPError && cause?.response.statusCode)
parts.push(`(HTTP ${cause.response.statusCode})`)
if (body.error) parts.push(`${body.error}:`)
if (body.message) parts.push(body.message)
if (body.assembly_ssl_url) parts.push(body.assembly_ssl_url)
const message = parts.join(' ')
super(message)
this.rawMessage = body.message
this.assemblyId = body.assembly_id
this.assemblySslUrl = body.assembly_ssl_url
this.code = body.error
this.cause = cause
}
}