-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAppError.js
More file actions
36 lines (29 loc) · 864 Bytes
/
Copy pathAppError.js
File metadata and controls
36 lines (29 loc) · 864 Bytes
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
/**
* Module dependencies
*/
const AppErrorCodes = {
serverError: 'SERVER_ERROR',
};
/**
* @desc Custom error class with Node + Express
* @param {String} message error
* @param {Object} { status, code }
*/
class AppError extends Error {
constructor(message, { details, status, code } = {}) {
super(message);
// Set HTTP status code
this.status = status || 500;
// Set API error code
this.code = code || AppErrorCodes.serverError;
// Ensures that stack trace uses our subclass name
this.name = this.constructor.name;
// Share clean messages for api feedback
if (details) this.details = details;
else this.details = [{ message }];
// Ensures the AppError subclass is sliced out of the
// stack trace dump for clarity
Error.captureStackTrace(this, this.constructor);
}
}
export default AppError;