forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppError.js
More file actions
29 lines (25 loc) · 791 Bytes
/
Copy pathAppError.js
File metadata and controls
29 lines (25 loc) · 791 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
'use strict';
const ERROR_CODES = Object.freeze({
VALIDATION_ERROR: { statusCode: 400 },
UNAUTHORIZED: { statusCode: 401 },
NOT_FOUND: { statusCode: 404 },
PAYLOAD_TOO_LARGE: { statusCode: 413 },
RATE_LIMITED: { statusCode: 429 },
UPSTREAM_ERROR: { statusCode: 502 },
INTERNAL_ERROR: { statusCode: 500 },
});
class AppError extends Error {
constructor(code, message, statusCode, details = {}) {
super(message);
if (!ERROR_CODES[code]) {
throw new Error(`Unknown application error code: ${code}`);
}
this.name = 'AppError';
this.code = code;
this.statusCode = statusCode || ERROR_CODES[code].statusCode;
this.details = details;
Error.captureStackTrace?.(this, AppError);
}
}
AppError.codes = ERROR_CODES;
module.exports = AppError;