UNPKG

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