1 | const debug = require('debug')('@feathersjs/errors');
|
2 |
|
3 | function FeathersError (msg, name, code, className, data) {
|
4 | msg = msg || 'Error';
|
5 |
|
6 | let errors;
|
7 | let message;
|
8 | let newData;
|
9 |
|
10 | if (msg instanceof Error) {
|
11 | message = msg.message || 'Error';
|
12 |
|
13 |
|
14 | if (msg.errors) {
|
15 | errors = msg.errors;
|
16 | }
|
17 | } else if (typeof msg === 'object') {
|
18 | message = msg.message || 'Error';
|
19 | data = msg;
|
20 | } else {
|
21 | message = msg;
|
22 | }
|
23 |
|
24 | if (data) {
|
25 |
|
26 |
|
27 |
|
28 | newData = JSON.parse(JSON.stringify(data));
|
29 |
|
30 | if (newData.errors) {
|
31 | errors = newData.errors;
|
32 | delete newData.errors;
|
33 | } else if (data.errors) {
|
34 |
|
35 |
|
36 |
|
37 | errors = JSON.parse(JSON.stringify(data.errors));
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | this.type = 'FeathersError';
|
45 | this.name = name;
|
46 | this.message = message;
|
47 | this.code = code;
|
48 | this.className = className;
|
49 | this.data = newData;
|
50 | this.errors = errors || {};
|
51 |
|
52 | debug(`${this.name}(${this.code}): ${this.message}`);
|
53 | debug(this.errors);
|
54 |
|
55 | if (Error.captureStackTrace) {
|
56 | Error.captureStackTrace(this, FeathersError);
|
57 | } else {
|
58 | this.stack = (new Error()).stack;
|
59 | }
|
60 | }
|
61 |
|
62 | function inheritsFrom (Child, Parent) {
|
63 | Child.prototype = Object.create(Parent.prototype);
|
64 | Child.prototype.constructor = Child;
|
65 | }
|
66 |
|
67 | inheritsFrom(FeathersError, Error);
|
68 |
|
69 |
|
70 |
|
71 | Object.defineProperty(FeathersError.prototype, 'toJSON', {
|
72 | value: function () {
|
73 | return {
|
74 | name: this.name,
|
75 | message: this.message,
|
76 | code: this.code,
|
77 | className: this.className,
|
78 | data: this.data,
|
79 | errors: this.errors
|
80 | };
|
81 | }
|
82 | });
|
83 |
|
84 |
|
85 | function BadRequest (message, data) {
|
86 | FeathersError.call(this, message, 'BadRequest', 400, 'bad-request', data);
|
87 | }
|
88 |
|
89 | inheritsFrom(BadRequest, FeathersError);
|
90 |
|
91 |
|
92 | function NotAuthenticated (message, data) {
|
93 | FeathersError.call(this, message, 'NotAuthenticated', 401, 'not-authenticated', data);
|
94 | }
|
95 |
|
96 | inheritsFrom(NotAuthenticated, FeathersError);
|
97 |
|
98 |
|
99 | function PaymentError (message, data) {
|
100 | FeathersError.call(this, message, 'PaymentError', 402, 'payment-error', data);
|
101 | }
|
102 |
|
103 | inheritsFrom(PaymentError, FeathersError);
|
104 |
|
105 |
|
106 | function Forbidden (message, data) {
|
107 | FeathersError.call(this, message, 'Forbidden', 403, 'forbidden', data);
|
108 | }
|
109 |
|
110 | inheritsFrom(Forbidden, FeathersError);
|
111 |
|
112 |
|
113 | function NotFound (message, data) {
|
114 | FeathersError.call(this, message, 'NotFound', 404, 'not-found', data);
|
115 | }
|
116 |
|
117 | inheritsFrom(NotFound, FeathersError);
|
118 |
|
119 |
|
120 | function MethodNotAllowed (message, data) {
|
121 | FeathersError.call(this, message, 'MethodNotAllowed', 405, 'method-not-allowed', data);
|
122 | }
|
123 |
|
124 | inheritsFrom(MethodNotAllowed, FeathersError);
|
125 |
|
126 |
|
127 | function NotAcceptable (message, data) {
|
128 | FeathersError.call(this, message, 'NotAcceptable', 406, 'not-acceptable', data);
|
129 | }
|
130 |
|
131 | inheritsFrom(NotAcceptable, FeathersError);
|
132 |
|
133 |
|
134 | function Timeout (message, data) {
|
135 | FeathersError.call(this, message, 'Timeout', 408, 'timeout', data);
|
136 | }
|
137 |
|
138 | inheritsFrom(Timeout, FeathersError);
|
139 |
|
140 |
|
141 | function Conflict (message, data) {
|
142 | FeathersError.call(this, message, 'Conflict', 409, 'conflict', data);
|
143 | }
|
144 |
|
145 | inheritsFrom(Conflict, FeathersError);
|
146 |
|
147 |
|
148 | function Gone (message, data) {
|
149 | FeathersError(this, message, 'Gone', 410, 'gone', data);
|
150 | }
|
151 |
|
152 | inheritsFrom(Gone, FeathersError);
|
153 |
|
154 |
|
155 | function LengthRequired (message, data) {
|
156 | FeathersError.call(this, message, 'LengthRequired', 411, 'length-required', data);
|
157 | }
|
158 |
|
159 | inheritsFrom(LengthRequired, FeathersError);
|
160 |
|
161 |
|
162 | function Unprocessable (message, data) {
|
163 | FeathersError.call(this, message, 'Unprocessable', 422, 'unprocessable', data);
|
164 | }
|
165 |
|
166 | inheritsFrom(Unprocessable, FeathersError);
|
167 |
|
168 |
|
169 | function TooManyRequests (message, data) {
|
170 | FeathersError.call(this, message, 'TooManyRequests', 429, 'too-many-requests', data);
|
171 | }
|
172 |
|
173 | inheritsFrom(TooManyRequests, FeathersError);
|
174 |
|
175 |
|
176 | function GeneralError (message, data) {
|
177 | FeathersError.call(this, message, 'GeneralError', 500, 'general-error', data);
|
178 | }
|
179 |
|
180 | inheritsFrom(GeneralError, FeathersError);
|
181 |
|
182 |
|
183 | function NotImplemented (message, data) {
|
184 | FeathersError.call(this, message, 'NotImplemented', 501, 'not-implemented', data);
|
185 | }
|
186 |
|
187 | inheritsFrom(NotImplemented, FeathersError);
|
188 |
|
189 |
|
190 | function BadGateway (message, data) {
|
191 | FeathersError.call(this, message, 'BadGateway', 502, 'bad-gateway', data);
|
192 | }
|
193 |
|
194 | inheritsFrom(BadGateway, FeathersError);
|
195 |
|
196 |
|
197 | function Unavailable (message, data) {
|
198 | FeathersError.call(this, message, 'Unavailable', 503, 'unavailable', data);
|
199 | }
|
200 |
|
201 | inheritsFrom(Unavailable, FeathersError);
|
202 |
|
203 | const errors = {
|
204 | FeathersError,
|
205 | BadRequest,
|
206 | NotAuthenticated,
|
207 | PaymentError,
|
208 | Forbidden,
|
209 | NotFound,
|
210 | MethodNotAllowed,
|
211 | NotAcceptable,
|
212 | Timeout,
|
213 | Conflict,
|
214 | Gone,
|
215 | LengthRequired,
|
216 | Unprocessable,
|
217 | TooManyRequests,
|
218 | GeneralError,
|
219 | NotImplemented,
|
220 | BadGateway,
|
221 | Unavailable,
|
222 | 400: BadRequest,
|
223 | 401: NotAuthenticated,
|
224 | 402: PaymentError,
|
225 | 403: Forbidden,
|
226 | 404: NotFound,
|
227 | 405: MethodNotAllowed,
|
228 | 406: NotAcceptable,
|
229 | 408: Timeout,
|
230 | 409: Conflict,
|
231 | 410: Gone,
|
232 | 411: LengthRequired,
|
233 | 422: Unprocessable,
|
234 | 429: TooManyRequests,
|
235 | 500: GeneralError,
|
236 | 501: NotImplemented,
|
237 | 502: BadGateway,
|
238 | 503: Unavailable
|
239 | };
|
240 |
|
241 | function convert (error) {
|
242 | if (!error) {
|
243 | return error;
|
244 | }
|
245 |
|
246 | const FeathersError = errors[error.name];
|
247 | const result = FeathersError
|
248 | ? new FeathersError(error.message, error.data)
|
249 | : new Error(error.message || error);
|
250 |
|
251 | if (typeof error === 'object') {
|
252 | Object.assign(result, error);
|
253 | }
|
254 |
|
255 | return result;
|
256 | }
|
257 |
|
258 | module.exports = Object.assign({ convert }, errors);
|