1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var LangUtils = require("./utils").LangUtils;
|
10 | var _ = require('lodash');
|
11 | var errors = require('./http-error-codes').Errors;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function 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 | }
|
26 | LangUtils.inherits(AbstractMethodError, Error);
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function 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 | }
|
42 | LangUtils.inherits(AbstractClassError, Error);
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function 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 | }
|
60 | LangUtils.inherits(CodedError, Error);
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function FileNotFoundError(msg) {
|
70 | FileNotFoundError.super_.bind(this)(msg, "EFOUND");
|
71 | }
|
72 | LangUtils.inherits(FileNotFoundError, CodedError);
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function 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 | }
|
103 | LangUtils.inherits(HttpError, CodedError);
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | HttpError.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 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function HttpBadRequestError(message, innerMessage) {
|
130 | HttpBadRequestError.super_.bind(this)(400, message, innerMessage);
|
131 | }
|
132 | LangUtils.inherits(HttpBadRequestError, HttpError);
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | function HttpNotFoundError(message, innerMessage) {
|
144 | HttpNotFoundError.super_.bind(this)(404, message, innerMessage);
|
145 | }
|
146 | LangUtils.inherits(HttpNotFoundError, HttpError);
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | function HttpMethodNotAllowedError(message, innerMessage) {
|
157 | HttpMethodNotAllowedError.super_.bind(this)(405, message, innerMessage);
|
158 | }
|
159 | LangUtils.inherits(HttpMethodNotAllowedError, HttpError);
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function HttpUnauthorizedError(message, innerMessage) {
|
170 | HttpUnauthorizedError.super_.bind(this)(401, message, innerMessage);
|
171 | }
|
172 | LangUtils.inherits(HttpUnauthorizedError, HttpError);
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function HttpNotAcceptableError(message, innerMessage) {
|
183 | HttpNotAcceptableError.super_.bind(this)(406, message, innerMessage);
|
184 | }
|
185 | LangUtils.inherits(HttpNotAcceptableError, HttpError);
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | function HttpRequestTimeoutError(message, innerMessage) {
|
196 | HttpRequestTimeoutError.super_.bind(this)(408, message, innerMessage);
|
197 | }
|
198 | LangUtils.inherits(HttpRequestTimeoutError, HttpError);
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function HttpConflictError(message, innerMessage) {
|
209 | HttpConflictError.super_.bind(this)(409, message, innerMessage);
|
210 | }
|
211 | LangUtils.inherits(HttpConflictError, HttpError);
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function HttpTokenExpiredError(message, innerMessage) {
|
222 | HttpTokenExpiredError.super_.bind(this)(498, message, innerMessage);
|
223 | }
|
224 | LangUtils.inherits(HttpTokenExpiredError, HttpError);
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | function HttpTokenRequiredError(message, innerMessage) {
|
235 | HttpTokenRequiredError.super_.bind(this)(498, message, innerMessage);
|
236 | }
|
237 | LangUtils.inherits(HttpTokenRequiredError, HttpError);
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function HttpForbiddenError(message, innerMessage) {
|
248 | HttpForbiddenError.super_.bind(this)(403, message, innerMessage);
|
249 | }
|
250 | LangUtils.inherits(HttpForbiddenError, HttpError);
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function HttpServerError(message, innerMessage) {
|
261 | HttpServerError.super_.bind(this)(500, message, innerMessage);
|
262 | }
|
263 | LangUtils.inherits(HttpServerError, HttpError);
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | function 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 | }
|
297 | LangUtils.inherits(DataError, CodedError);
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | function DataNotFoundError(message, innerMessage, model) {
|
308 | DataNotFoundError.super_.bind(this)('EFOUND', message || 'The requested data was not found.', innerMessage, model);
|
309 | this.statusCode = 404;
|
310 | }
|
311 | LangUtils.inherits(DataNotFoundError, DataError);
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | function AccessDeniedError(message, innerMessage, model) {
|
322 | AccessDeniedError.super_.bind(this)('EACCESS', ('Access Denied' || message) , innerMessage, model);
|
323 | this.statusCode = 401;
|
324 | }
|
325 | LangUtils.inherits(AccessDeniedError, DataError);
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | function UniqueConstraintError(message, innerMessage, model) {
|
336 | UniqueConstraintError.super_.bind(this)('EUNQ', message || 'A unique constraint violated', innerMessage, model);
|
337 | }
|
338 | LangUtils.inherits(UniqueConstraintError, DataError);
|
339 |
|
340 |
|
341 | if (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 |