Completed
Push — master ( 2357d9...32357a )
by Esaú
01:33
created

Throwable.js (3 issues)

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") throw new Error("parameter 'name' must be a 'string'");
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20 60
        this.name = name;
21
    }
22
    // check parameter 'message'
23 1402
    if (message != null) {
24 702
        if (typeof message != "string") throw new Error("parameter 'message' must be a 'string'");
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
25 54
        this.message = message;
26
    }
27
    // check parameter 'code'
28 754
    if (code != null) {
29 606
        if (typeof code != "number") throw new Error("parameter 'code' must be a 'number'");
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
30 39
        this.code = code;
31
    }
32
};
33
34
// :: INHERITANCE
35
36
// set the prototype chain parent to 'Object'
37 1
Throwable.prototype = Object.create(Object.prototype);
38
39
// set the prototype chain constructor
40 1
Throwable.prototype.constructor = Throwable;
41
42
// :: PROTOTYPE
43
44
// set the default name
45 1
Throwable.prototype.name = null;
46
47
// set the default message
48 1
Throwable.prototype.message = null;
49
50
// set the default code
51 1
Throwable.prototype.code = null;
52
53
// set the toString method
54 1
Throwable.prototype.toString = function () {
55 24
    let str = (this.name != null ? this.name : "Throwable");
56 24
    if (this.code != null) {
57 4
        str += " (0x" + this.code.toString(16) + ")";
58
    }
59 24
    str += ": " + (this.message != null ? this.message : "thrown") + ".";
60 24
    return str;
61
};
62
63
// returns a native 'Error'
64 1
Throwable.prototype.native = function () {
65 24
    let str = (this.name != null ? this.name : "Throwable");
66 24
    if (this.code != null) {
67 4
        str += " (0x" + this.code.toString(16) + ")";
68
    }
69 24
    str += ": " + (this.message != null ? this.message : "thrown");
70 24
    return new Error(str);
71
};
72
73
// :: EXPORT
74
75
// export the Throwable 'class'
76
module.exports = Throwable;