Total Complexity | 12 |
Complexity/F | 1.33 |
Lines of Code | 60 |
Function Count | 9 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import Validator from '@hckrnews/validator'; |
||
2 | import errorSchema from './schemas/error'; |
||
3 | |||
4 | const validator = new Validator(errorSchema); |
||
|
|||
5 | |||
6 | export default (error = Error) => |
||
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 }; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | get hasErrors() { |
||
43 | return validator.errors.length > 0; |
||
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); |
||
59 | } |
||
60 | }; |
||
61 |