-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseError.js
41 lines (32 loc) · 1.02 KB
/
baseError.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
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-undef */
/* eslint-disable max-classes-per-file */
// Base error classes to extend from.
// Use assertions to catch programming errors.
// https://en.wikipedia.org/wiki/Assertion_%28software_development%29#Comparison_with_error_handling
const assert = require('assert');
class ApplicationError extends Error {
constructor(message, options = {}) {
assert(typeof message === 'string');
assert(typeof options === 'object');
assert(options !== null);
super(message);
// Attach relevant information to the error instance
// (e.g., the username).
for (const [key, value] of Object.entries(options)) {
this[key] = value;
}
}
get name() {
return this.constructor.name;
}
}
class OutgoingRequestError extends ApplicationError {}
class DatabaseError extends ApplicationError {}
class UserFacingError extends ApplicationError {}
module.exports = {
ApplicationError,
DatabaseError,
OutgoingRequestError,
UserFacingError,
};