-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.js
52 lines (46 loc) · 1.41 KB
/
errors.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
45
46
47
48
49
50
51
52
const util = require('util');
const { ObjectDefineProperties } = require('./primordials.js');
// Port from node core lib/internal/errors.js
class ERR_INVALID_ARG_TYPE extends TypeError {
constructor(message, actual) {
super();
if (actual == null) {
message += `. Received ${actual}`;
} else if (typeof actual === 'function' && actual.name) {
message += `. Received function ${actual.name}`;
} else if (typeof actual === 'object') {
if (actual.constructor && actual.constructor.name) {
message += `. Received an instance of ${actual.constructor.name}`;
} else {
const inspected = util.inspect(actual, { depth: -1 });
message += `. Received ${inspected}`;
}
} else {
let inspected = util.inspect(actual, { colors: false });
if (inspected.length > 25) {
inspected = `${inspected.slice(0, 25)}...`;
}
message += `. Received type ${typeof actual} (${inspected})`;
}
this.code = this.constructor.name;
ObjectDefineProperties(this, {
message: {
value: message,
enumerable: false,
writable: true,
configurable: true,
},
toString: {
value() {
return `${this.name} [${this.code}]: ${this.message}`;
},
enumerable: false,
writable: true,
configurable: true,
},
});
}
}
module.exports = {
ERR_INVALID_ARG_TYPE,
};