Passed
Branch feature/first-draft (521840)
by Pieter Epeüs
02:27
created

src/app-error.js   A

Complexity

Total Complexity 12
Complexity/F 1.33

Size

Lines of Code 60
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 36
mnd 3
bc 3
fnc 9
dl 0
loc 60
rs 10
bpm 0.3333
cpm 1.3333
noi 5
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A app-error.js ➔ setValues 0 11 2
A app-error.js ➔ values 0 10 1
A app-error.js ➔ errorStatus 0 3 1
A app-error.js ➔ validate 0 3 1
A app-error.js ➔ hasErrors 0 3 1
A app-error.js ➔ name 0 3 1
A app-error.js ➔ constructor 0 9 3
A app-error.js ➔ status 0 3 2
1
import Validator from '@hckrnews/validator';
2
import errorSchema from './schemas/error';
3
4
const validator = new Validator(errorSchema);
0 ignored issues
show
Unused Code introduced by
The constant validator seems to be never used. Consider removing it.
Loading history...
5
6
export default (error = Error) =>
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
7
    class AppError extends error {
8
        constructor({ value = null, type = null, message }) {
9
            super(message);
10
11
            if (Error.captureStackTrace) {
12
                Error.captureStackTrace(this, AppError);
13
            }
14
15
            this.setValues({ value, type });
16
        }
17
18
        get name() {
19
            return 'AppError';
20
        }
21
22
        get errorStatus() {
23
            return 500;
24
        }
25
26
        get status() {
27
            return this.hasErrors ? 500 : this.errorStatus;
28
        }
29
30
        setValues({ value, type }) {
31
            this.value = value;
32
            this.type = type;
33
            this.date = new Date();
34
35
            if (!this.validate()) {
36
                this.type = Error;
37
                this.message = 'Invalid error';
38
                this.value = { errors: validator.errors, values: this.values };
0 ignored issues
show
Bug introduced by
The variable validator seems to be never declared. If this is a global, consider adding a /** global: validator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
39
            }
40
        }
41
42
        get hasErrors() {
43
            return validator.errors.length > 0;
0 ignored issues
show
Bug introduced by
The variable validator seems to be never declared. If this is a global, consider adding a /** global: validator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
44
        }
45
46
        get values() {
47
            return {
48
                name: this.name,
49
                message: this.message,
50
                value: this.value,
51
                status: this.errorStatus,
52
                type: this.type,
53
                date: this.date,
54
            };
55
        }
56
57
        validate() {
58
            return validator.validate(this.values);
0 ignored issues
show
Bug introduced by
The variable validator seems to be never declared. If this is a global, consider adding a /** global: validator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
        }
60
    };
61