1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var LangUtils = require("./utils").LangUtils;
|
11 | var _ = require('lodash');
|
12 | var errors = require('./http-error-codes').Errors;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function AbstractMethodError(msg) {
|
23 | AbstractMethodError.super_.bind(this)(msg);
|
24 | this.message = msg || 'Class does not implement inherited abstract method.';
|
25 | if (typeof Error.captureStackTrace === 'function') {
|
26 | Error.captureStackTrace(this, this.constructor);
|
27 | }
|
28 | }
|
29 | LangUtils.inherits(AbstractMethodError, Error);
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function AbstractClassError(msg) {
|
39 | AbstractClassError.super_.bind(this)(msg);
|
40 | this.message = msg || 'An abstract class cannot be instantiated.';
|
41 | if (typeof Error.captureStackTrace === 'function') {
|
42 | Error.captureStackTrace(this, this.constructor);
|
43 | }
|
44 | }
|
45 | LangUtils.inherits(AbstractClassError, Error);
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function CodedError(msg, code) {
|
56 | CodedError.super_.bind(this)(msg);
|
57 | this.message = msg;
|
58 | this.code = code;
|
59 | if (typeof Error.captureStackTrace === 'function') {
|
60 | Error.captureStackTrace(this, this.constructor);
|
61 | }
|
62 | }
|
63 | LangUtils.inherits(CodedError, Error);
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function FileNotFoundError(msg) {
|
73 | FileNotFoundError.super_.bind(this)(msg, "E_FOUND");
|
74 | }
|
75 | LangUtils.inherits(FileNotFoundError, CodedError);
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function HttpError(status, message, innerMessage) {
|
87 | HttpError.super_.bind(this)(message, "E_HTTP");
|
88 | var finalStatus = _.isNumber(status) ? status : 500;
|
89 | var err = _.find(errors, function(x) {
|
90 | return x.statusCode === finalStatus;
|
91 | });
|
92 | if (err) {
|
93 | this.title = err.title;
|
94 | this.message = message || err.message;
|
95 | this.statusCode = err.statusCode;
|
96 | }
|
97 | else {
|
98 | this.title = 'Internal Server Error';
|
99 | this.message = message || 'The server encountered an internal error and was unable to complete the request.';
|
100 | this.statusCode = finalStatus
|
101 | }
|
102 | if (typeof innerMessage !== 'undefined') {
|
103 | this.innerMessage = innerMessage;
|
104 | }
|
105 | }
|
106 | LangUtils.inherits(HttpError, CodedError);
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | HttpError.create = function(err) {
|
113 | if (_.isNil(err)) {
|
114 | return new HttpError(500);
|
115 | }
|
116 | if (Object.prototype.hasOwnProperty.call(err,'statusCode')) {
|
117 | return _.assign(new HttpError(err.statusCode, err.message), err);
|
118 | }
|
119 | else {
|
120 | return _.assign(new HttpError(500, err.message), err);
|
121 | }
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function HttpBadRequestError(message, innerMessage) {
|
133 | HttpBadRequestError.super_.bind(this)(400, message, innerMessage);
|
134 | }
|
135 | LangUtils.inherits(HttpBadRequestError, HttpError);
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function HttpNotFoundError(message, innerMessage) {
|
147 | HttpNotFoundError.super_.bind(this)(404, message, innerMessage);
|
148 | }
|
149 | LangUtils.inherits(HttpNotFoundError, HttpError);
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function HttpMethodNotAllowedError(message, innerMessage) {
|
160 | HttpMethodNotAllowedError.super_.bind(this)(405, message, innerMessage);
|
161 | }
|
162 | LangUtils.inherits(HttpMethodNotAllowedError, HttpError);
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | function HttpUnauthorizedError(message, innerMessage) {
|
173 | HttpUnauthorizedError.super_.bind(this)(401, message, innerMessage);
|
174 | }
|
175 | LangUtils.inherits(HttpUnauthorizedError, HttpError);
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | function HttpNotAcceptableError(message, innerMessage) {
|
186 | HttpNotAcceptableError.super_.bind(this)(406, message, innerMessage);
|
187 | }
|
188 | LangUtils.inherits(HttpNotAcceptableError, HttpError);
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function HttpRequestTimeoutError(message, innerMessage) {
|
199 | HttpRequestTimeoutError.super_.bind(this)(408, message, innerMessage);
|
200 | }
|
201 | LangUtils.inherits(HttpRequestTimeoutError, HttpError);
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | function HttpConflictError(message, innerMessage) {
|
212 | HttpConflictError.super_.bind(this)(409, message, innerMessage);
|
213 | }
|
214 | LangUtils.inherits(HttpConflictError, HttpError);
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | function HttpTokenExpiredError(message, innerMessage) {
|
225 | HttpTokenExpiredError.super_.bind(this)(498, message, innerMessage);
|
226 | }
|
227 | LangUtils.inherits(HttpTokenExpiredError, HttpError);
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | function HttpTokenRequiredError(message, innerMessage) {
|
238 | HttpTokenRequiredError.super_.bind(this)(498, message, innerMessage);
|
239 | }
|
240 | LangUtils.inherits(HttpTokenRequiredError, HttpError);
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function HttpForbiddenError(message, innerMessage) {
|
251 | HttpForbiddenError.super_.bind(this)(403, message, innerMessage);
|
252 | }
|
253 | LangUtils.inherits(HttpForbiddenError, HttpError);
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function HttpServerError(message, innerMessage) {
|
264 | HttpServerError.super_.bind(this)(500, message, innerMessage);
|
265 | }
|
266 | LangUtils.inherits(HttpServerError, HttpError);
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | function DataError(code, message, innerMessage, model, field, additionalData) {
|
286 | DataError.super_.bind(this)(message, code);
|
287 | this.code = code || 'E_DATA';
|
288 | if (typeof model !== 'undefined') {
|
289 | this.model = model;
|
290 | }
|
291 | if (typeof field !== 'undefined') {
|
292 | this.field = field;
|
293 | }
|
294 | this.message = message || 'A general data error occurred.';
|
295 | if (typeof innerMessage !== 'undefined') {
|
296 | this.innerMessage = innerMessage;
|
297 | }
|
298 | this.additionalData = additionalData;
|
299 | }
|
300 | LangUtils.inherits(DataError, CodedError);
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | function NotNullError(message, innerMessage, model,field) {
|
315 | NotNullError.super_.bind(this)('E_NULL', message || 'A value is required.', innerMessage, model,field);
|
316 | this.statusCode = 409;
|
317 | }
|
318 | LangUtils.inherits(NotNullError, DataError);
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | function DataNotFoundError(message, innerMessage, model) {
|
329 | DataNotFoundError.super_.bind(this)('E_FOUND', message || 'The requested data was not found.', innerMessage, model);
|
330 | this.statusCode = 404;
|
331 | }
|
332 | LangUtils.inherits(DataNotFoundError, DataError);
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | function AccessDeniedError(message, innerMessage, model) {
|
343 | AccessDeniedError.super_.bind(this)('E_ACCESS', ('Access Denied' || message) , innerMessage, model);
|
344 | this.statusCode = 401;
|
345 | }
|
346 | LangUtils.inherits(AccessDeniedError, DataError);
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 | function UniqueConstraintError(message, innerMessage, model) {
|
357 | UniqueConstraintError.super_.bind(this)('E_UNIQUE', message || 'A unique constraint violated', innerMessage, model);
|
358 | }
|
359 | LangUtils.inherits(UniqueConstraintError, DataError);
|
360 |
|
361 |
|
362 | if (typeof exports !== 'undefined') {
|
363 | module.exports.AbstractMethodError = AbstractMethodError;
|
364 | module.exports.AbstractClassError = AbstractClassError;
|
365 | module.exports.FileNotFoundError = FileNotFoundError;
|
366 | module.exports.HttpError = HttpError;
|
367 | module.exports.HttpBadRequestError = HttpBadRequestError;
|
368 | module.exports.HttpNotFoundError = HttpNotFoundError;
|
369 | module.exports.HttpMethodNotAllowedError = HttpMethodNotAllowedError;
|
370 | module.exports.HttpNotAcceptableError = HttpNotAcceptableError;
|
371 | module.exports.HttpConflictError = HttpConflictError;
|
372 | module.exports.HttpRequestTimeoutError = HttpRequestTimeoutError;
|
373 | module.exports.HttpTokenExpiredError = HttpTokenExpiredError;
|
374 | module.exports.HttpTokenRequiredError = HttpTokenRequiredError;
|
375 | module.exports.HttpUnauthorizedError = HttpUnauthorizedError;
|
376 | module.exports.HttpForbiddenError = HttpForbiddenError;
|
377 | module.exports.HttpServerError = HttpServerError;
|
378 | module.exports.DataError = DataError;
|
379 | module.exports.DataNotFoundError = DataNotFoundError;
|
380 | module.exports.NotNullError = NotNullError;
|
381 | module.exports.AccessDeniedError = AccessDeniedError;
|
382 | module.exports.UniqueConstraintError = UniqueConstraintError;
|
383 | }
|