-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (33 loc) · 1.03 KB
/
index.js
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
/**
* A collection of useful error objects. (maybe)
*/
function ErrorFactory(name, default_status) {
var HttpError = function (options) {
if (!(this instanceof HttpError)) {
return new HttpError(options);
}
options = options || {}
var parent_args = [ options.message || name ];
Error.apply(this, parent_args);
this.status = options.status || 500;
this.method = options.method || "UNKNOWN";
this.code = options.code || "UNKNOWN";
this.validation = options.validation || {};
if (options.obj_id) {
this.message += ": (" + options.obj_id + ") ";
}
if (options.obj_name) {
this.message += options.obj_name;
}
}
HttpError.name = name;
HttpError.prototype = Error.prototype;
HttpError.constructor = HttpError;
return HttpError;
}
module.exports = {
"ServerError": ErrorFactory("ServerError", 500),
"NotFound": ErrorFactory("NotFound", 404),
"NotAuthenticated": ErrorFactory("NotAuthenticated", 401),
"NotAuthorized": ErrorFactory("NotAuthorized", 403)
};