aggregate-error
Advanced tools
Comparing version 1.0.0 to 2.0.0
26
index.js
@@ -9,16 +9,28 @@ 'use strict'; | ||
constructor(errors) { | ||
// Even though strings are iterable, we don't allow them to prevent subtle user mistakes | ||
if (!errors[Symbol.iterator] || typeof errors === 'string') { | ||
throw new TypeError(`Expected input to be iterable, got ${typeof errors}`); | ||
if (!Array.isArray(errors)) { | ||
throw new TypeError(`Expected input to be an Array, got ${typeof errors}`); | ||
} | ||
errors = Array.from(errors).map(err => err instanceof Error ? err : new Error(err)); | ||
errors = [...errors].map(error => { | ||
if (error instanceof Error) { | ||
return error; | ||
} | ||
let message = errors.map(err => cleanInternalStack(cleanStack(err.stack))).join('\n'); | ||
if (error !== null && typeof error === 'object') { | ||
// Handle plain error objects with message property and/or possibly other metadata | ||
return Object.assign(new Error(error.message), error); | ||
} | ||
return new Error(error); | ||
}); | ||
let message = errors.map(error => cleanInternalStack(cleanStack(error.stack))).join('\n'); | ||
message = '\n' + indentString(message, 4); | ||
super(message); | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.name = 'AggregateError'; | ||
Object.defineProperty(this, '_errors', {value: errors}); | ||
} | ||
* [Symbol.iterator]() { | ||
@@ -25,0 +37,0 @@ for (const error of this._errors) { |
{ | ||
"name": "aggregate-error", | ||
"version": "1.0.0", | ||
"description": "Create an error from multiple errors", | ||
"license": "MIT", | ||
"repository": "sindresorhus/aggregate-error", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"aggregate", | ||
"error", | ||
"err", | ||
"combine", | ||
"multiple", | ||
"many", | ||
"collection", | ||
"iterable", | ||
"iterator" | ||
], | ||
"dependencies": { | ||
"clean-stack": "^1.0.0", | ||
"indent-string": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "aggregate-error", | ||
"version": "2.0.0", | ||
"description": "Create an error from multiple errors", | ||
"license": "MIT", | ||
"repository": "sindresorhus/aggregate-error", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"aggregate", | ||
"error", | ||
"err", | ||
"combine", | ||
"multiple", | ||
"many", | ||
"collection", | ||
"iterable", | ||
"iterator" | ||
], | ||
"dependencies": { | ||
"clean-stack": "^2.0.0", | ||
"indent-string": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.0.1", | ||
"xo": "^0.23.0" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # aggregate-error [![Build Status](https://travis-ci.org/sindresorhus/aggregate-error.svg?branch=master)](https://travis-ci.org/sindresorhus/aggregate-error) | ||
``` | ||
$ npm install --save aggregate-error | ||
$ npm install aggregate-error | ||
``` | ||
@@ -19,5 +19,5 @@ | ||
const err = new AggregateError([new Error('foo'), 'bar']); | ||
const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]); | ||
throw err; | ||
throw error; | ||
/* | ||
@@ -29,2 +29,4 @@ AggregateError: | ||
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) | ||
Error: baz | ||
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) | ||
at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3) | ||
@@ -42,7 +44,8 @@ at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) | ||
for (const el of err) { | ||
console.log(el); | ||
for (const individualError of error) { | ||
console.log(individualError); | ||
} | ||
//=> [Error: foo] | ||
//=> [Error: bar] | ||
//=> [Error: baz] | ||
``` | ||
@@ -55,11 +58,14 @@ | ||
Returns an `Error` that is also an [`iterator`](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Iterators_and_Generators) for the individual errors. | ||
Returns an `Error` that is also an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables) for the individual errors. | ||
#### errors | ||
Type: `Iterable<Error|string>` | ||
Type: `Array<Error|Object|string>` | ||
If a string, a new `Error` is created with the string as the error message.<br> | ||
If a non-Error object, a new `Error` is created with all properties from the object copied over. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
4747
32
67
+ Addedclean-stack@2.2.0(transitive)
- Removedclean-stack@1.3.0(transitive)
Updatedclean-stack@^2.0.0