-
Notifications
You must be signed in to change notification settings - Fork 41
/
APIError.ts
executable file
·121 lines (103 loc) · 3.24 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import depd = require("depd");
const deprecate = depd("json-api");
export type APIErrorJSON = {
status?: string;
code?: string;
title?: string;
detail?: string;
links?: any;
paths?: any;
}
export type Opts = {
status?: string | number;
code?: string | number;
title?: string;
detail?: string;
links?: object;
paths?: string[];
};
export const displaySafe = Symbol('isJSONAPIDisplayReady');
// TODO: refactor args list to be:
// constructor(title, status, [code, detail, links, paths])
// This matches the standard Error constructor and makes sense intuitively.
// TODO: turn on noImplicitAny, fix errors
export default class APIError extends Error {
public status?: string;
public code?: string;
public title?: string;
public detail?: string;
public links?: any;
public paths?: any;
constructor(opts: Opts);
constructor(
status?: Opts['status'], code?: Opts['code'], title?: Opts['title'],
detail?: Opts['detail'], links?: Opts['links'], paths?: Opts['paths']
);
constructor(...args) {
super();
if(Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor || APIError);
}
// Use a Proxy to handle coercing status, code etc.
// to strings on set (including below in the constructor).
// Because we're setting the validated properties directly on obj,
// ownKeys will work correctly.
const res = new Proxy(this, {
set(obj, prop, value) {
const coercePropToString =
["status", "code", "title", "detail"].indexOf(<string>prop) > -1;
obj[prop] = coercePropToString
? (value == null ? undefined : String(value))
: value;
return true;
}
});
// Construct from object format
if(args.length === 1 && typeof args[0] === 'object') {
Object.assign(res, args[0]);
}
// Construct from list of arguments
else {
[res.status, res.code, res.title, res.detail, res.links, res.paths] = args;
}
return res;
}
toJSON(): APIErrorJSON {
return {...(this as object)};
}
/**
* Creates a JSON-API Compliant Error Object from a JS Error object
*
*/
static fromError(err) {
const ErrorConstructor = this || APIError; // in case `this` isn't bound.
const fallbackTitle =
"An unknown error occurred while trying to process this request.";
if(err instanceof APIError) {
return err;
}
// If the error is marked as ready for JSON API display, it's secure
// to read values off it and show them to the user. (Note: most of
// the args below will probably be null/undefined, but that's fine.)
else if(err[displaySafe] || err.isJSONAPIDisplayReady) {
if(err.isJSONAPIDisplayReady) {
deprecate(
"isJSONAPIDisplayReady magic property: " +
"use the APIError.displaySafe symbol instead."
);
}
return new ErrorConstructor(
err.status || err.statusCode || 500,
err.code,
err.title || fallbackTitle,
err.detail || err.details || (err.message ? err.message : undefined),
err.links,
err.paths
);
}
// Otherwise, we just show a generic error message.
else {
return new ErrorConstructor({ status: 500, title: fallbackTitle });
}
}
}