1 | // Throwable.js |
||
2 | "use strict"; |
||
3 | |||
4 | // :: BASIC SETUP |
||
5 | |||
6 | /** |
||
7 | * The <tt>Throwable</tt> class is the superclass of all errors and exceptions. |
||
8 | * @param {String} name - The name of the thrown object. |
||
9 | * @param {String} message - The message describing the thrown object. |
||
10 | * @param {Number} code - The unique code that identifies the cause of the thrown object. |
||
11 | * @augments Object |
||
12 | * @constructor |
||
13 | * @see https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html |
||
14 | */ |
||
15 | 1 | const Throwable = function (name, message, code) { |
|
16 | // call the 'parent class' |
||
17 | 12952 | Object.call(this); |
|
18 | // check parameter 'name' |
||
19 | 12952 | if (name !== null && name !== undefined) { |
|
20 | 2868 | if (typeof name !== "string") { |
|
21 | 2628 | throw new Error("parameter 'name' must be a 'string'"); |
|
22 | } |
||
23 | 240 | this.name = name; |
|
24 | } |
||
25 | // check parameter 'message' |
||
26 | 10324 | if (message !== null && message !== undefined) { |
|
27 | 4944 | if (typeof message !== "string") { |
|
28 | 4320 | throw new Error("parameter 'message' must be a 'string'"); |
|
29 | } |
||
30 | 624 | this.message = message; |
|
31 | } |
||
32 | // check parameter 'code' |
||
33 | 6004 | if (code !== null && code !== undefined) { |
|
34 | 4224 | if (typeof code !== "number") { |
|
35 | 3780 | throw new Error("parameter 'code' must be a 'number'"); |
|
36 | } |
||
37 | 444 | this.code = code; |
|
38 | } |
||
39 | }; |
||
40 | |||
41 | // :: INHERITANCE |
||
42 | |||
43 | // set the prototype chain parent to 'Object' |
||
44 | 1 | Throwable.prototype = Object.create(Object.prototype); |
|
45 | |||
46 | // set the prototype chain constructor |
||
47 | 1 | Throwable.prototype.constructor = Throwable; |
|
48 | |||
49 | // :: PROTOTYPE |
||
50 | |||
51 | /** |
||
52 | * The name used to identify a thrown object. |
||
53 | * @type {String} |
||
54 | * @default |
||
55 | */ |
||
56 | 1 | Throwable.prototype.name = "Throwable"; |
|
57 | |||
58 | /** |
||
59 | * The description of the cause of the thrown object. |
||
60 | * @type {String} |
||
61 | * @default |
||
62 | */ |
||
63 | 1 | Throwable.prototype.message = "thrown"; |
|
64 | |||
65 | /** |
||
66 | * A short code that helps identifying a thrown object. |
||
67 | * @type {Number} |
||
68 | * @default |
||
69 | */ |
||
70 | 1 | Throwable.prototype.code = null; |
|
71 | |||
72 | /** |
||
73 | * Gets the string representation of the {@linkcode Throwable} instance. |
||
74 | * @returns {String} The string representation of the thrown object. |
||
75 | * @override |
||
76 | */ |
||
77 | 1 | Throwable.prototype.toString = function () { |
|
78 | 428 | let str = this.name; |
|
79 | 428 | if (this.code !== null && this.code !== undefined) { |
|
80 | 64 | str += " (0x" + this.code.toString(16) + ')'; |
|
81 | } |
||
82 | 428 | str += ": " + this.message + '.'; |
|
83 | 428 | return str; |
|
84 | }; |
||
85 | |||
86 | /** |
||
87 | * Throws an <tt>Error</tt> with the message of the {@linkcode Throwable} instance. |
||
88 | * @return {Error} The <tt>Error</tt> instance with the same message as the thrown object. |
||
89 | * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error |
||
90 | */ |
||
91 | 1 | Throwable.prototype.native = function () { |
|
92 | 288 | return new Error(this.message); |
|
93 | }; |
||
94 | |||
95 | // :: EXPORT |
||
96 | |||
97 | // export the Throwable 'class' |
||
98 | module.exports = Throwable; |