-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (44 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict'
const kErrors = Symbol('kErrors')
module.exports = function (errors) {
errors = errors.filter(defined)
if (errors.length === 0) return
if (errors.length === 1) return errors[0]
return new CombinedError(errors)
}
class CombinedError extends Error {
constructor (errors) {
const unique = new Set(errors.map(getMessage).filter(Boolean))
const message = Array.from(unique).join('; ')
super(message)
value(this, 'name', 'CombinedError')
value(this, kErrors, errors)
getter(this, 'stack', () => errors.map(getStack).join('\n\n'))
getter(this, 'transient', () => errors.length > 0 && errors.every(transient))
getter(this, 'expected', () => errors.length > 0 && errors.every(expected))
}
[Symbol.iterator] () {
return this[kErrors][Symbol.iterator]()
}
}
function value (obj, prop, value) {
Object.defineProperty(obj, prop, { value })
}
function getter (obj, prop, get) {
Object.defineProperty(obj, prop, { get })
}
function defined (err) {
return err != null
}
function getMessage (err) {
return err.message
}
function getStack (err) {
return err.stack
}
function transient (err) {
return err.transient === true
}
function expected (err) {
return err.expected === true
}