/
errors.js
51 lines (48 loc) · 1.1 KB
/
errors.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
50
51
const debug = require('debug')('Errors');
const httpStatus = require('http-status');
/**
* @extends Error
*/
class ExtendableError extends Error {
/**
* Creates an instance of ExtendableError.
* @param {string} message
* @param {string} status
* @param {boolean} isPublic
* @memberof ExtendableError
*/
constructor(message, status, isPublic) {
super(message);
this.name = this.constructor.name;
this.message = message;
this.status = status;
this.isPublic = isPublic;
Error.captureStackTrace(this, this.constructor.name);
}
}
/**
* APIError
*/
class APIError extends ExtendableError {
/**
* Creates an API error.
* @param {string} message - Error message.
* @param {number} status - HTTP status code of error.
* @param {boolean} isPublic - Is message visible to user.
*/
constructor(
message,
status = httpStatus.INTERNAL_SERVER_ERROR,
isPublic = false
) {
super(message, status, isPublic);
}
}
const errors = {
log(error) {
// Log to file / external logger
debug(error);
},
APIError: APIError
};
module.exports = errors;