Completed
Push — master ( 32357a...c39652 )
by Esaú
01:50
created

Throwable.js ➔ Throwable   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
c 3
b 0
f 0
nc 8
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 10
rs 4.8196
nop 3

How to fix   Complexity   

Complexity

Complex classes like Throwable.js ➔ Throwable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 && name !== undefined) {
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 && message !== undefined) {
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 && code !== undefined) {
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 !== undefined ? this.name : "Throwable");
62 24
    if (this.code !== null && this.code !== undefined) {
63 4
        str += " (0x" + this.code.toString(16) + ")";
64
    }
65 24
    str += ": " + (this.message !== null && this.message !== undefined ? 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 !== undefined ? this.name : "Throwable");
72 24
    if (this.code !== null && this.code !== undefined) {
73 4
        str += " (0x" + this.code.toString(16) + ")";
74
    }
75 24
    str += ": " + (this.message !== null && this.message !== undefined ? this.message : "thrown");
76 24
    return new Error(str);
77
};
78
79
// :: EXPORT
80
81
// export the Throwable 'class'
82
module.exports = Throwable;