-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (34 loc) · 997 Bytes
/
index.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
const {toString} = Object.prototype;
const isError = value => toString.call(value) === '[object Error]';
export class PresentableError extends Error {
constructor(message, {cause} = {}) {
super();
if (message instanceof PresentableError) {
return message; // eslint-disable-line no-constructor-return
}
if (typeof message !== 'string') {
throw new TypeError('Message required.');
}
this.name = 'PresentableError';
this.message = message;
this.cause = cause;
}
get isPresentable() {
return true;
}
}
export function isPresentableError(value) {
return value && isError(value) && value.isPresentable === true;
}
export function makePresentableError(error) {
if (!isError(error)) {
throw new TypeError('The value is not an error.');
}
if (isPresentableError(error)) {
return;
}
if (!Object.isExtensible(error)) {
throw new Error('The error is non-extensible and cannot be edited.');
}
Object.defineProperty(error, 'isPresentable', {value: true});
}