1
|
|
|
// Throwable.js |
2
|
|
|
"use strict"; |
3
|
|
|
|
4
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
5
|
|
|
* Copyright (C) EgaLabs, Inc - All Rights Reserved * |
6
|
|
|
* Unauthorized copying of this file, either completely or partially, via any medium, is strictly prohibited. * |
7
|
|
|
* Proprietary and confidential. * |
8
|
|
|
* Written by Esaú García Sánchez-Torija <[email protected]>, March 2017 * |
9
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
10
|
|
|
|
11
|
|
|
// :: BASIC SETUP |
12
|
|
|
|
13
|
|
|
// empty 'class' |
14
|
1 |
|
const Throwable = function (name, message, code) { |
15
|
|
|
// call the 'parent class' |
16
|
2059 |
|
Object.call(this); |
17
|
|
|
// check parameter 'name' |
18
|
2059 |
|
if (name != null) { |
19
|
717 |
|
if (typeof name !== "string") { |
20
|
657 |
|
throw new Error("parameter 'name' must be a 'string'"); |
21
|
|
|
} |
22
|
60 |
|
this.name = name; |
23
|
|
|
} |
24
|
|
|
// check parameter 'message' |
25
|
1402 |
|
if (message != null) { |
26
|
702 |
|
if (typeof message != "string") { |
27
|
648 |
|
throw new Error("parameter 'message' must be a 'string'"); |
28
|
|
|
} |
29
|
54 |
|
this.message = message; |
30
|
|
|
} |
31
|
|
|
// check parameter 'code' |
32
|
754 |
|
if (code != null) { |
33
|
606 |
|
if (typeof code != "number") { |
34
|
567 |
|
throw new Error("parameter 'code' must be a 'number'"); |
35
|
|
|
} |
36
|
39 |
|
this.code = code; |
37
|
|
|
} |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
// :: INHERITANCE |
41
|
|
|
|
42
|
|
|
// set the prototype chain parent to 'Object' |
43
|
1 |
|
Throwable.prototype = Object.create(Object.prototype); |
44
|
|
|
|
45
|
|
|
// set the prototype chain constructor |
46
|
1 |
|
Throwable.prototype.constructor = Throwable; |
47
|
|
|
|
48
|
|
|
// :: PROTOTYPE |
49
|
|
|
|
50
|
|
|
// set the default name |
51
|
1 |
|
Throwable.prototype.name = null; |
52
|
|
|
|
53
|
|
|
// set the default message |
54
|
1 |
|
Throwable.prototype.message = null; |
55
|
|
|
|
56
|
|
|
// set the default code |
57
|
1 |
|
Throwable.prototype.code = null; |
58
|
|
|
|
59
|
|
|
// set the toString method |
60
|
1 |
|
Throwable.prototype.toString = function () { |
61
|
24 |
|
let str = (this.name != null ? this.name : "Throwable"); |
62
|
24 |
|
if (this.code != null) { |
63
|
4 |
|
str += " (0x" + this.code.toString(16) + ")"; |
64
|
|
|
} |
65
|
24 |
|
str += ": " + (this.message != null ? this.message : "thrown") + "."; |
66
|
24 |
|
return str; |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
// returns a native 'Error' |
70
|
1 |
|
Throwable.prototype.native = function () { |
71
|
24 |
|
let str = (this.name != null ? this.name : "Throwable"); |
72
|
24 |
|
if (this.code != null) { |
73
|
4 |
|
str += " (0x" + this.code.toString(16) + ")"; |
74
|
|
|
} |
75
|
24 |
|
str += ": " + (this.message != null ? this.message : "thrown"); |
76
|
24 |
|
return new Error(str); |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
// :: EXPORT |
80
|
|
|
|
81
|
|
|
// export the Throwable 'class' |
82
|
|
|
module.exports = Throwable; |