src/codingame/error.js   A
last analyzed

Size

Lines of Code 40

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 40
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A error.js ➔ ??? 0 11 3
1
/**
2
 * @file Module 'codingame/error'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Error for Codingame API
7
 * @module codingame/error
8
 * @see {@link CodingameError}
9
 */
10
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