-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiError.js
34 lines (32 loc) · 1.12 KB
/
ApiError.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
const { INTERNAL_SERVER_ERROR, getStatusText } = require('http-status-codes');
module.exports = class ApiError extends Error {
/**
* Creates an instance of ApiError
*
* @param {number|ApiErrorOptions} [options] HTTP status code to respond with (defaults to 500), or an object
* with code and message properties
*/
constructor(options) {
let status;
let message;
if (typeof options === 'object') {
({ status, message } = options);
if (!message) message = getStatusText(status);
} else if (typeof options === 'number') {
status = options;
message = getStatusText(status);
} else {
status = INTERNAL_SERVER_ERROR;
message = getStatusText(INTERNAL_SERVER_ERROR);
}
super(message);
this.name = this.constructor.name;
this.status = status;
}
};
/**
* @typedef {Object} ApiErrorOptions
* @property {number} status HTTP status code to respond with
* @property {string} message Custom message to respond with, defaults to standard message
* for the provided code, as defined by [http-status-codes](https://www.npmjs.com/package/http-status-codes)
*/