-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy patherror.ts
75 lines (64 loc) · 1.9 KB
/
error.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// import { } from 'got';
import { RequestError } from 'got/dist/source';
import { URL } from 'url';
export type TwilioApiError = {
code: number;
message: string;
more_info: string;
status: number;
};
function toTwilioApiError(response: unknown): TwilioApiError | undefined {
if (typeof response !== 'object') {
return undefined;
}
return response as TwilioApiError;
}
/**
* Explictly removes username and password from a URL.
* @param unfilteredUrl any URL string
*/
export function filterUrl(unfilteredUrl: string | undefined): string {
if (!unfilteredUrl) {
return '';
}
const url = new URL(unfilteredUrl);
url.username = '';
url.password = '';
return url.toString();
}
/**
* Throws the error it receives and if it's an HTTP Error it will convert it to a ClientApiError
*
*/
export function convertApiErrorsAndThrow(err: any): never {
if (err.name === 'HTTPError') {
err = new ClientApiError(err);
}
throw err;
}
/**
* An Error wrapper to provide more useful error information without exposing credentials
*/
export class ClientApiError extends Error {
public details?: TwilioApiError;
public code?: number | string;
public url?: string;
constructor(requestError: unknown) {
super('Unknown Error');
if (requestError instanceof RequestError) {
const twilioApiErrorInfo = toTwilioApiError(requestError.response?.body);
const message = twilioApiErrorInfo?.message || requestError.message;
Error.captureStackTrace(this, this.constructor);
this.name = requestError.name;
this.message = message;
this.code = twilioApiErrorInfo?.code || requestError.response?.statusCode;
this.url = filterUrl(requestError.response?.requestUrl);
if (requestError.name === 'HTTPError' && twilioApiErrorInfo) {
this.name = 'TwilioApiError';
this.details = {
...twilioApiErrorInfo,
};
}
}
}
}