UNPKG

12.2 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9///
10var LangUtils = require("./utils").LangUtils;
11var _ = require('lodash');
12var errors = require('./http-error-codes').Errors;
13
14
15/**
16 * @classdesc Thrown when an application tries to call an abstract method.
17 * @class
18 * @param {string=} msg
19 * @constructor
20 * @augments Error
21 */
22function 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}
29LangUtils.inherits(AbstractMethodError, Error);
30
31/**
32 * @classdesc Thrown when an application tries to instantiate an abstract class.
33 * @class
34 * @param {string=} msg
35 * @constructor
36 * @extends Error
37 */
38function 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}
45LangUtils.inherits(AbstractClassError, Error);
46
47/**
48 * @classdesc Represents an error with a code.
49 * @class
50 * @param {string} msg
51 * @param {string} code
52 * @constructor
53 * @extends Error
54 */
55function 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}
63LangUtils.inherits(CodedError, Error);
64
65/**
66 * @classdesc Thrown when an application tries to access a file which does not exist.
67 * @class
68 * @param {string=} msg
69 * @constructor
70 * @extends CodedError
71 */
72function FileNotFoundError(msg) {
73 FileNotFoundError.super_.bind(this)(msg, "EFOUND");
74}
75LangUtils.inherits(FileNotFoundError, CodedError);
76
77/**
78 * @classdesc Represents an HTTP error.
79 * @class
80 * @param {number} status
81 * @param {string=} message
82 * @param {string=} innerMessage
83 * @constructor
84 * @extends CodedError
85 */
86function HttpError(status, message, innerMessage) {
87 HttpError.super_.bind(this)(message, "EHTTP");
88 var hstatus = _.isNumber(status) ? status : 500;
89 var err = _.find(errors, function(x) {
90 return x.statusCode === hstatus;
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 = hstatus
101 }
102 if (typeof innerMessage !== 'undefined') {
103 this.innerMessage = innerMessage;
104 }
105}
106LangUtils.inherits(HttpError, CodedError);
107
108/**
109 * @param {Error} err
110 * @returns {Error|HttpError}
111 */
112HttpError.create = function(err) {
113 if (_.isNil(err)) {
114 return new HttpError(500);
115 }
116 if (err.hasOwnProperty('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 * @classdesc Represents a 400 HTTP Bad Request error.
126 * @class
127 * @param {string=} message
128 * @param {string=} innerMessage
129 * @constructor
130 * @extends HttpError
131 */
132function HttpBadRequestError(message, innerMessage) {
133 HttpBadRequestError.super_.bind(this)(400, message, innerMessage);
134}
135LangUtils.inherits(HttpBadRequestError, HttpError);
136
137/**
138 * @classdesc Represents a 404 HTTP Not Found error.
139 * @class
140 * @param {string=} message
141 * @param {string=} innerMessage
142 * @constructor
143 * @property {string} resource - Gets or sets the requested resource which could not to be found
144 * @extends HttpError
145 */
146function HttpNotFoundError(message, innerMessage) {
147 HttpNotFoundError.super_.bind(this)(404, message, innerMessage);
148}
149LangUtils.inherits(HttpNotFoundError, HttpError);
150
151/**
152 * @classdesc Represents a 405 HTTP Method Not Allowed error.
153 * @class
154 * @param {string=} message
155 * @param {string=} innerMessage
156 * @constructor
157 * @extends HttpError
158 */
159function HttpMethodNotAllowedError(message, innerMessage) {
160 HttpMethodNotAllowedError.super_.bind(this)(405, message, innerMessage);
161}
162LangUtils.inherits(HttpMethodNotAllowedError, HttpError);
163
164/**
165 * @classdesc Represents a 401 HTTP Unauthorized error.
166 * @class
167 * @param {string=} message
168 * @param {string=} innerMessage
169 * @constructor
170 * @extends HttpError
171 */
172function HttpUnauthorizedError(message, innerMessage) {
173 HttpUnauthorizedError.super_.bind(this)(401, message, innerMessage);
174}
175LangUtils.inherits(HttpUnauthorizedError, HttpError);
176
177/**
178 * @classdesc HTTP 406 Not Acceptable exception class
179 * @class
180 * @param {string=} message
181 * @param {string=} innerMessage
182 * @constructor
183 * @extends HttpError
184 */
185function HttpNotAcceptableError(message, innerMessage) {
186 HttpNotAcceptableError.super_.bind(this)(406, message, innerMessage);
187}
188LangUtils.inherits(HttpNotAcceptableError, HttpError);
189
190/**
191 * @classdesc HTTP 408 RequestTimeout exception class
192 * @class
193 * @param {string=} message
194 * @param {string=} innerMessage
195 * @constructor
196 * @extends HttpError
197 */
198function HttpRequestTimeoutError(message, innerMessage) {
199 HttpRequestTimeoutError.super_.bind(this)(408, message, innerMessage);
200}
201LangUtils.inherits(HttpRequestTimeoutError, HttpError);
202
203/**
204 * @classdesc HTTP 409 Conflict exception class
205 * @class
206 * @param {string=} message
207 * @param {string=} innerMessage
208 * @constructor
209 * @extends HttpError
210 */
211function HttpConflictError(message, innerMessage) {
212 HttpConflictError.super_.bind(this)(409, message, innerMessage);
213}
214LangUtils.inherits(HttpConflictError, HttpError);
215
216/**
217 * @classdesc HTTP 498 Token Expired exception class
218 * @class
219 * @param {string=} message
220 * @param {string=} innerMessage
221 * @constructor
222 * @extends HttpError
223 */
224function HttpTokenExpiredError(message, innerMessage) {
225 HttpTokenExpiredError.super_.bind(this)(498, message, innerMessage);
226}
227LangUtils.inherits(HttpTokenExpiredError, HttpError);
228
229/**
230 * @classdesc HTTP 499 Token Required exception class
231 * @class
232 * @param {string=} message
233 * @param {string=} innerMessage
234 * @constructor
235 * @extends HttpError
236 */
237function HttpTokenRequiredError(message, innerMessage) {
238 HttpTokenRequiredError.super_.bind(this)(498, message, innerMessage);
239}
240LangUtils.inherits(HttpTokenRequiredError, HttpError);
241
242/**
243 * @classdesc Represents a 403 HTTP Forbidden error.
244 * @class
245 * @param {string=} message
246 * @param {string=} innerMessage
247 * @constructor
248 * @extends HttpError
249 */
250function HttpForbiddenError(message, innerMessage) {
251 HttpForbiddenError.super_.bind(this)(403, message, innerMessage);
252}
253LangUtils.inherits(HttpForbiddenError, HttpError);
254
255/**
256 * @classdesc Represents a 500 HTTP Internal Server error.
257 * @class
258 * @param {string=} message
259 * @param {string=} innerMessage
260 * @constructor
261 * @extends HttpError
262 */
263function HttpServerError(message, innerMessage) {
264 HttpServerError.super_.bind(this)(500, message, innerMessage);
265}
266LangUtils.inherits(HttpServerError, HttpError);
267
268/**
269 * @classdesc Extends Error object for throwing exceptions on data operations
270 * @class
271 * @param {string=} code - A string that represents an error code
272 * @param {string=} message - The error message
273 * @param {string=} innerMessage - The error inner message
274 * @param {string=} model - The target model
275 * @param {string=} field - The target field
276 * @param {*} additionalData - Additional data associated with this error
277 * @constructor
278 * @property {string} code - A string that represents an error code e.g. EDATA
279 * @property {string} message - The error message.
280 * @property {string} innerMessage - The error inner message.
281 * @property {number} status - A number that represents an error status. This error status may be used for throwing the appropriate HTTP error.
282 * @property {*} additionalData - Additional data associated with this error
283 * @augments CodedError
284 */
285function DataError(code, message, innerMessage, model, field, additionalData) {
286 DataError.super_.bind(this)(message, code);
287 this.code = code || 'EDATA';
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 occured.';
295 if (typeof innerMessage !== 'undefined') {
296 this.innerMessage = innerMessage;
297 }
298 this.additionalData = additionalData;
299}
300LangUtils.inherits(DataError, CodedError);
301
302
303
304
305/**
306 * Thrown when an application attempts to access a data object that cannot be found.
307 * @param {string=} message - The error message
308 * @param {string=} innerMessage - The error inner message
309 * @param {string=} model - The target model
310 * @param {string=} field - The target field
311 * @constructor
312 * @extends DataError
313 */
314function NotNullError(message, innerMessage, model,field) {
315 NotNullError.super_.bind(this)('ENULL', message || 'A value is required.', innerMessage, model,field);
316 this.statusCode = 409;
317}
318LangUtils.inherits(NotNullError, DataError);
319
320/**
321 * Thrown when an application attempts to access a data object that cannot be found.
322 * @param {string=} message - The error message
323 * @param {string=} innerMessage - The error inner message
324 * @param {string=} model - The target model
325 * @constructor
326 * @extends DataError
327 */
328function DataNotFoundError(message, innerMessage, model) {
329 DataNotFoundError.super_.bind(this)('EFOUND', message || 'The requested data was not found.', innerMessage, model);
330 this.statusCode = 404;
331}
332LangUtils.inherits(DataNotFoundError, DataError);
333
334/**
335 * Thrown when a data object operation is denied
336 * @param {string=} message - The error message
337 * @param {string=} innerMessage - The error inner message
338 * @param {string=} model - The target model
339 * @constructor
340 * @extends DataError
341 */
342function AccessDeniedError(message, innerMessage, model) {
343 AccessDeniedError.super_.bind(this)('EACCESS', ('Access Denied' || message) , innerMessage, model);
344 this.statusCode = 401;
345}
346LangUtils.inherits(AccessDeniedError, DataError);
347
348/**
349 * Thrown when a unique constraint is being violated
350 * @param {string=} message - The error message
351 * @param {string=} innerMessage - The error inner message
352 * @param {string=} model - The target model
353 * @constructor
354 * @extends DataError
355 */
356function UniqueConstraintError(message, innerMessage, model) {
357 UniqueConstraintError.super_.bind(this)('EUNQ', message || 'A unique constraint violated', innerMessage, model);
358}
359LangUtils.inherits(UniqueConstraintError, DataError);
360
361
362if (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}
\No newline at end of file