|
1
|
|
|
// Exception.js |
|
2
|
|
|
"use strict"; |
|
3
|
|
|
|
|
4
|
|
|
// :: DEPENDENCIES |
|
5
|
|
|
|
|
6
|
|
|
// load native dependencies |
|
7
|
1 |
|
const path = require("path"); |
|
8
|
|
|
|
|
9
|
|
|
// load local dependencies |
|
10
|
1 |
|
const Throwable = require(path.join(__dirname, "Throwable.js")); |
|
11
|
|
|
|
|
12
|
|
|
// :: BASIC SETUP |
|
13
|
|
|
|
|
14
|
|
|
// empty 'class' |
|
15
|
1 |
|
const Exception = function (name, message, code) { |
|
16
|
2060 |
|
Throwable.call(this, name, message, code); |
|
17
|
|
|
}; |
|
18
|
|
|
|
|
19
|
|
|
// :: INHERITANCE |
|
20
|
|
|
|
|
21
|
|
|
// set the prototype chain parent to the Throwable 'class' |
|
22
|
1 |
|
Exception.prototype = Object.create(Throwable.prototype); |
|
23
|
|
|
|
|
24
|
|
|
// set the prototype chain constructor |
|
25
|
1 |
|
Exception.prototype.constructor = Exception; |
|
26
|
|
|
|
|
27
|
|
|
// :: PROTOTYPE |
|
28
|
|
|
|
|
29
|
|
|
// set the toString method |
|
30
|
1 |
|
Exception.prototype.toString = function () { |
|
31
|
24 |
|
let str = (this.name !== null && this.name !== undefined ? this.name : "Exception"); |
|
32
|
24 |
|
if (this.code !== null && this.code !== undefined) { |
|
33
|
4 |
|
str += " (0x" + this.code.toString(16) + ")"; |
|
34
|
|
|
} |
|
35
|
24 |
|
str += ": " + (this.message !== null && this.message !== undefined ? this.message : "thrown") + "."; |
|
36
|
24 |
|
return str; |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
// returns a native 'Error' |
|
40
|
1 |
|
Exception.prototype.native = function () { |
|
41
|
24 |
|
let str = (this.name !== null && this.name !== undefined ? this.name : "Exception"); |
|
42
|
24 |
|
if (this.code !== null && this.code !== undefined) { |
|
43
|
4 |
|
str += " (0x" + this.code.toString(16) + ")"; |
|
44
|
|
|
} |
|
45
|
24 |
|
str += ": " + (this.message !== null && this.message !== undefined ? this.message : "thrown"); |
|
46
|
24 |
|
return new Error(str); |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
// :: EXPORT |
|
50
|
|
|
|
|
51
|
|
|
// export the Exception 'class' |
|
52
|
|
|
module.exports = Exception; |