-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (44 loc) · 1.18 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
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const errors = require('restify-errors');
function errorExist(type) {
return Object.prototype.hasOwnProperty.call(errors, type);
}
/**
* Throws a specific Restify error.
* @param {string} type The type of error to throw
* @param {string} msg An error message
* @param {(string|number)} errno An unique error id code to let clients handle the error
* @param {...} [debug] An indefinite number of contex-debug information to collect
*/
function throwError(type, msg, errno, ...debug) {
debug = debug || [];
if (!errorExist(type)) {
type = 'InternalServerError';
debug.push('Invalid error type provided:' + type);
}
const err = new errors[type]({
message: msg,
context: {
errno,
debug
}
});
err.body.errno = errno;
return err;
}
/**
* Checks if a specific Restify error was thrown.
* @param {object} err The object to check
* @param {string} [type] The type of error that the object should be instance of
* @returns {boolean} true if the type match, false otherwise
*/
function errorThrown(err, type) {
if (errorExist(type)) {
return err instanceof errors[type];
}
return false;
}
module.exports = {
throw: throwError,
thrown: errorThrown
};