-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (35 loc) · 1.02 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
'use strict'
/**
* Module Dependencies
*/
var Custom = require('custom-error-instance')
var uniq = require('lodash.uniqby')
/**
* Use a custom error type
*/
var MultiError = Custom('MultiError')
/**
* Export `Error`
*/
module.exports = error
/**
* Initialize an error
*/
function error (errors) {
if (!(this instanceof error)) return new error(errors)
errors = Array.isArray(errors) ? errors : [ errors ]
errors = uniq(errors, function (err) { return err.stack })
if (errors.length === 1) return errors[0]
var multierror = new MultiError({
message: errors.map(function (err) { return err.message }).join('; '),
errors: errors.reduce(function (errs, err) { return errs.concat(err.errors || err) }, []),
})
// lazily get/set the stack
multierror.__defineGetter__('stack', function() {
return errors.map(function (err) { return err.stack }).join('\n\n')
})
multierror.__defineSetter__('stack', function(value) {
return [value].concat(multierror.stack).join('\n\n')
})
return multierror
}