Total Complexity | 5 |
Complexity/F | 5 |
Lines of Code | 41 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | /** |
||
23 | import CodingameError from '../error.js'; |
||
24 | |||
25 | let name = `fail-expected`; |
||
26 | |||
27 | /** |
||
28 | * Attempt to parse the body of a successful request to Codingame test API. |
||
29 | * This function will try to map a response for a failed test because of a unexpected result for the test. |
||
30 | * |
||
31 | * @function parse |
||
32 | * @param {Object} body Body of the response |
||
33 | * @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful |
||
34 | * @throws {Error} Throw is parsing failed |
||
35 | * @instance |
||
36 | */ |
||
37 | let parse = (body) => { |
||
38 | try { |
||
39 | let { |
||
40 | "output": output, |
||
41 | "comparison": { |
||
42 | "success": success, |
||
43 | "expected": expected, |
||
44 | "found": found |
||
45 | } |
||
46 | } = body; |
||
47 | if (!success && expected !== undefined && found !== undefined) { |
||
48 | let error = new CodingameError(`Expected <${body.comparison.expected}> but found <${body.comparison.found}>`); |
||
49 | return Promise.reject(error); |
||
50 | } else { |
||
51 | throw `Success value should be false when expected and found properties are provided`; |
||
52 | } |
||
53 | } catch (error) { |
||
54 | let message = `The body is not of response type '${name}'\n`; |
||
55 | message += error.message; |
||
56 | throw new Error(message); |
||
57 | } |
||
58 | }; |
||
59 | |||
60 | export default { |
||
61 | "name": name, |
||
62 | "parse": parse |
||
63 | }; |
||
64 |