Lines of Code | 40 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | /** |
||
11 | class CodingameError extends Error { |
||
12 | /** |
||
13 | * Create an error for reporting Codingame test fails. |
||
14 | * Represents errors in a Codingame test which is not a technical error: |
||
15 | * - the test didn't pass |
||
16 | * - the bundle didn't compile |
||
17 | * - the objective of the problem has not been reached |
||
18 | * - etc. |
||
19 | * @extends Error |
||
20 | * @constructs CodingameError |
||
21 | * @param {string|Error} error - The message or the parent Error |
||
22 | */ |
||
23 | constructor(error) { |
||
24 | if (typeof error === `string`) { |
||
25 | super(error); |
||
26 | } else if (error instanceof Error) { |
||
27 | super(error.message); |
||
28 | this.stacktrace = error.stacktrace; |
||
29 | } else { |
||
30 | super(); |
||
31 | } |
||
32 | this.name = `CodingameError`; |
||
33 | }; |
||
34 | |||
35 | /** |
||
36 | * Attach some random metadata to the error, preserving `name` and `message` properties |
||
37 | * @method attach |
||
38 | * @param {Object} metadata - Some random object |
||
39 | * @memberof CodingameError |
||
40 | * @instance |
||
41 | */ |
||
42 | attach(metadata) { |
||
43 | let copy = metadata; |
||
44 | delete copy.name; |
||
45 | delete copy.message; |
||
46 | Object.assign(this, copy); |
||
47 | }; |
||
48 | } |
||
49 | |||
50 | export default CodingameError; |
||
51 |