Completed
Pull Request — master (#32)
by Jean
55s
created

error.js ➔ ???   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 9.4285
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
		this.metadata = {};
34
	};
35
36
	/**
37
	 * Attach some random metadata to the error
38
	 * @method attach
39
	 * @param {Object} metadata - Some random object
40
	 * @memberof CodingameError
41
	 * @instance
42
	 */
43
	attach(metadata) {
44
		Object.assign(this.metadata, metadata);
45
	};
46
}
47
48
export default CodingameError;
49