-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.js
More file actions
383 lines (355 loc) · 11.9 KB
/
Copy patherrors.js
File metadata and controls
383 lines (355 loc) · 11.9 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* @license
* MOST Web Framework 2.0 Codename Blueshift
* Copyright (c) 2017, THEMOST LP All rights reserved
*
* Use of this source code is governed by an BSD-3-Clause license that can be
* found in the LICENSE file at https://themost.io/license
*/
///
var LangUtils = require("./utils").LangUtils;
var _ = require('lodash');
var errors = require('./http-error-codes').Errors;
/**
* @classdesc Thrown when an application tries to call an abstract method.
* @class
* @param {string=} msg
* @constructor
* @augments Error
*/
function AbstractMethodError(msg) {
AbstractMethodError.super_.bind(this)(msg);
this.message = msg || 'Class does not implement inherited abstract method.';
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
}
}
LangUtils.inherits(AbstractMethodError, Error);
/**
* @classdesc Thrown when an application tries to instantiate an abstract class.
* @class
* @param {string=} msg
* @constructor
* @extends Error
*/
function AbstractClassError(msg) {
AbstractClassError.super_.bind(this)(msg);
this.message = msg || 'An abstract class cannot be instantiated.';
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
}
}
LangUtils.inherits(AbstractClassError, Error);
/**
* @classdesc Represents an error with a code.
* @class
* @param {string} msg
* @param {string} code
* @constructor
* @extends Error
*/
function CodedError(msg, code) {
CodedError.super_.bind(this)(msg);
this.message = msg;
this.code = code;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
}
}
LangUtils.inherits(CodedError, Error);
/**
* @classdesc Thrown when an application tries to access a file which does not exist.
* @class
* @param {string=} msg
* @constructor
* @extends CodedError
*/
function FileNotFoundError(msg) {
FileNotFoundError.super_.bind(this)(msg, "E_FOUND");
}
LangUtils.inherits(FileNotFoundError, CodedError);
/**
* @classdesc Represents an HTTP error.
* @class
* @param {number} status
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends CodedError
*/
function HttpError(status, message, innerMessage) {
HttpError.super_.bind(this)(message, "E_HTTP");
var finalStatus = _.isNumber(status) ? status : 500;
var err = _.find(errors, function(x) {
return x.statusCode === finalStatus;
});
if (err) {
this.title = err.title;
this.message = message || err.message;
this.statusCode = err.statusCode;
}
else {
this.title = 'Internal Server Error';
this.message = message || 'The server encountered an internal error and was unable to complete the request.';
this.statusCode = finalStatus
}
if (typeof innerMessage !== 'undefined') {
this.innerMessage = innerMessage;
}
}
LangUtils.inherits(HttpError, CodedError);
/**
* @param {Error|HttpError} err
* @returns {Error|HttpError}
*/
HttpError.create = function(err) {
if (_.isNil(err)) {
return new HttpError(500);
}
if (Object.prototype.hasOwnProperty.call(err,'statusCode')) {
return _.assign(new HttpError(err.statusCode, err.message), err);
}
else {
return _.assign(new HttpError(500, err.message), err);
}
};
/**
* @classdesc Represents a 400 HTTP Bad Request error.
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpBadRequestError(message, innerMessage) {
HttpBadRequestError.super_.bind(this)(400, message, innerMessage);
}
LangUtils.inherits(HttpBadRequestError, HttpError);
/**
* @classdesc Represents a 404 HTTP Not Found error.
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @property {string} resource - Gets or sets the requested resource which could not to be found
* @extends HttpError
*/
function HttpNotFoundError(message, innerMessage) {
HttpNotFoundError.super_.bind(this)(404, message, innerMessage);
}
LangUtils.inherits(HttpNotFoundError, HttpError);
/**
* @classdesc Represents a 405 HTTP Method Not Allowed error.
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpMethodNotAllowedError(message, innerMessage) {
HttpMethodNotAllowedError.super_.bind(this)(405, message, innerMessage);
}
LangUtils.inherits(HttpMethodNotAllowedError, HttpError);
/**
* @classdesc Represents a 401 HTTP Unauthorized error.
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpUnauthorizedError(message, innerMessage) {
HttpUnauthorizedError.super_.bind(this)(401, message, innerMessage);
}
LangUtils.inherits(HttpUnauthorizedError, HttpError);
/**
* @classdesc HTTP 406 Not Acceptable exception class
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpNotAcceptableError(message, innerMessage) {
HttpNotAcceptableError.super_.bind(this)(406, message, innerMessage);
}
LangUtils.inherits(HttpNotAcceptableError, HttpError);
/**
* @classdesc HTTP 408 RequestTimeout exception class
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpRequestTimeoutError(message, innerMessage) {
HttpRequestTimeoutError.super_.bind(this)(408, message, innerMessage);
}
LangUtils.inherits(HttpRequestTimeoutError, HttpError);
/**
* @classdesc HTTP 409 Conflict exception class
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpConflictError(message, innerMessage) {
HttpConflictError.super_.bind(this)(409, message, innerMessage);
}
LangUtils.inherits(HttpConflictError, HttpError);
/**
* @classdesc HTTP 498 Token Expired exception class
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpTokenExpiredError(message, innerMessage) {
HttpTokenExpiredError.super_.bind(this)(498, message, innerMessage);
}
LangUtils.inherits(HttpTokenExpiredError, HttpError);
/**
* @classdesc HTTP 499 Token Required exception class
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpTokenRequiredError(message, innerMessage) {
HttpTokenRequiredError.super_.bind(this)(498, message, innerMessage);
}
LangUtils.inherits(HttpTokenRequiredError, HttpError);
/**
* @classdesc Represents a 403 HTTP Forbidden error.
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpForbiddenError(message, innerMessage) {
HttpForbiddenError.super_.bind(this)(403, message, innerMessage);
}
LangUtils.inherits(HttpForbiddenError, HttpError);
/**
* @classdesc Represents a 500 HTTP Internal Server error.
* @class
* @param {string=} message
* @param {string=} innerMessage
* @constructor
* @extends HttpError
*/
function HttpServerError(message, innerMessage) {
HttpServerError.super_.bind(this)(500, message, innerMessage);
}
LangUtils.inherits(HttpServerError, HttpError);
/**
* @classdesc Extends Error object for throwing exceptions on data operations
* @class
* @param {string=} code - A string that represents an error code
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @param {string=} model - The target model
* @param {string=} field - The target field
* @param {*} additionalData - Additional data associated with this error
* @constructor
* @property {string} code - A string that represents an error code e.g. E_DATA
* @property {string} message - The error message.
* @property {string} innerMessage - The error inner message.
* @property {number} status - A number that represents an error status. This error status may be used for throwing the appropriate HTTP error.
* @property {*} additionalData - Additional data associated with this error
* @augments CodedError
*/
function DataError(code, message, innerMessage, model, field, additionalData) {
DataError.super_.bind(this)(message, code);
this.code = code || 'E_DATA';
if (typeof model !== 'undefined') {
this.model = model;
}
if (typeof field !== 'undefined') {
this.field = field;
}
this.message = message || 'A general data error occurred.';
if (typeof innerMessage !== 'undefined') {
this.innerMessage = innerMessage;
}
this.additionalData = additionalData;
}
LangUtils.inherits(DataError, CodedError);
/**
* Thrown when an application attempts to access a data object that cannot be found.
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @param {string=} model - The target model
* @param {string=} field - The target field
* @constructor
* @extends DataError
*/
function NotNullError(message, innerMessage, model,field) {
NotNullError.super_.bind(this)('E_NULL', message || 'A value is required.', innerMessage, model,field);
this.statusCode = 409;
}
LangUtils.inherits(NotNullError, DataError);
/**
* Thrown when an application attempts to access a data object that cannot be found.
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @param {string=} model - The target model
* @constructor
* @extends DataError
*/
function DataNotFoundError(message, innerMessage, model) {
DataNotFoundError.super_.bind(this)('E_FOUND', message || 'The requested data was not found.', innerMessage, model);
this.statusCode = 404;
}
LangUtils.inherits(DataNotFoundError, DataError);
/**
* Thrown when a data object operation is denied
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @param {string=} model - The target model
* @constructor
* @extends DataError
*/
function AccessDeniedError(message, innerMessage, model) {
AccessDeniedError.super_.bind(this)('E_ACCESS', ('Access Denied' || message) , innerMessage, model);
this.statusCode = 401;
}
LangUtils.inherits(AccessDeniedError, DataError);
/**
* Thrown when a unique constraint is being violated
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @param {string=} model - The target model
* @constructor
* @extends DataError
*/
function UniqueConstraintError(message, innerMessage, model) {
UniqueConstraintError.super_.bind(this)('E_UNIQUE', message || 'A unique constraint violated', innerMessage, model);
}
LangUtils.inherits(UniqueConstraintError, DataError);
if (typeof exports !== 'undefined') {
module.exports.AbstractMethodError = AbstractMethodError;
module.exports.AbstractClassError = AbstractClassError;
module.exports.FileNotFoundError = FileNotFoundError;
module.exports.HttpError = HttpError;
module.exports.HttpBadRequestError = HttpBadRequestError;
module.exports.HttpNotFoundError = HttpNotFoundError;
module.exports.HttpMethodNotAllowedError = HttpMethodNotAllowedError;
module.exports.HttpNotAcceptableError = HttpNotAcceptableError;
module.exports.HttpConflictError = HttpConflictError;
module.exports.HttpRequestTimeoutError = HttpRequestTimeoutError;
module.exports.HttpTokenExpiredError = HttpTokenExpiredError;
module.exports.HttpTokenRequiredError = HttpTokenRequiredError;
module.exports.HttpUnauthorizedError = HttpUnauthorizedError;
module.exports.HttpForbiddenError = HttpForbiddenError;
module.exports.HttpServerError = HttpServerError;
module.exports.DataError = DataError;
module.exports.DataNotFoundError = DataNotFoundError;
module.exports.NotNullError = NotNullError;
module.exports.AccessDeniedError = AccessDeniedError;
module.exports.UniqueConstraintError = UniqueConstraintError;
}