1
|
|
|
import { assert } from 'chai'; |
2
|
|
|
import { Decorator } from '../entry'; |
3
|
|
|
import Logger from '../Logger'; |
4
|
|
|
|
5
|
|
|
suite('Errors'); |
6
|
|
|
|
7
|
|
|
test('Negative: function throws error', function () { |
8
|
|
|
const logger = new Logger(); |
9
|
|
|
const decorator = new Decorator({ logger }); |
10
|
|
|
|
11
|
|
|
const error = new Error('Please set correct params'); |
12
|
|
|
const decorated = decorator()(function () { |
13
|
|
|
throw error; |
14
|
|
|
}); |
15
|
|
|
|
16
|
|
|
assert.throws(decorated.bind(null, 5), error); |
17
|
|
|
assert.isEmpty(logger.stack.verbose); |
18
|
|
|
assert.include(logger.stack.error[0].error, error.toString()); |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
View Code Duplication |
test('Negative: async function throws error', async function () { |
|
|
|
|
22
|
|
|
const logger = new Logger(); |
23
|
|
|
const decorator = new Decorator({ logger }); |
24
|
|
|
|
25
|
|
|
const error = new Error('care toy degree shirt heart'); |
26
|
|
|
const decorated = decorator()(async function double() { |
27
|
|
|
await new Promise((res, rej) => { |
28
|
|
|
setTimeout(() => { |
29
|
|
|
return rej(error); |
30
|
|
|
}, 50); |
31
|
|
|
}); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
await decorated(); |
36
|
|
|
} catch (error_) { |
37
|
|
|
assert.equal(error_.toString(), error.toString()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
assert.isEmpty(logger.stack.verbose); |
41
|
|
|
assert.include(logger.stack.error[0].error, error.toString()); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
View Code Duplication |
test('Negative: function return rejected promise', async function () { |
|
|
|
|
45
|
|
|
const logger = new Logger(); |
46
|
|
|
const decorator = new Decorator({ logger }); |
47
|
|
|
|
48
|
|
|
const error = new Error('mill share from cattle muscle musical structure progress'); |
49
|
|
|
const decorated = decorator()(function double() { |
50
|
|
|
return new Promise((res, rej) => { |
51
|
|
|
setTimeout(() => { |
52
|
|
|
return rej(error); |
53
|
|
|
}, 50); |
54
|
|
|
}); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
await decorated(); |
59
|
|
|
} catch (error_) { |
60
|
|
|
assert.equal(error_.toString(), error.toString()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
assert.isEmpty(logger.stack.verbose); |
64
|
|
|
assert.include(logger.stack.error[0].error, error.toString()); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
function errored(err) { |
68
|
|
|
throw err; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
test('Negative: different errorLevels on different errors', function () { |
72
|
|
|
const logger = new Logger({ levels: [ 'warn', 'error' ] }); |
73
|
|
|
const decorator = new Decorator({ |
74
|
|
|
logger, |
75
|
|
|
errorLevel : ({ error }) => error.message === 'NOT_FOUND' ? 'warn' : 'error' |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
const decorated = decorator()(errored); |
80
|
|
|
|
81
|
|
|
assert.throws(decorated.bind(null, new Error('NOT_FOUND'))); |
82
|
|
|
assert.isEmpty(logger.stack.error); |
83
|
|
|
assert.notEmpty(logger.stack.warn); |
84
|
|
|
|
85
|
|
|
logger.clear(); |
86
|
|
|
|
87
|
|
|
assert.throws(decorated.bind(null, new Error('FORMAT_ERROR'))); |
88
|
|
|
assert.notEmpty(logger.stack.error); |
89
|
|
|
assert.isEmpty(logger.stack.warn); |
90
|
|
|
}); |
91
|
|
|
|