├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── package.json ├── license ├── index.js ├── index.d.ts ├── test.js └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/aggregate-error 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: actions/setup-node@v3 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectAssignable} from 'tsd'; 2 | import AggregateError from './index.js'; 3 | 4 | const aggregateError = new AggregateError([ 5 | new Error('foo'), 6 | {foo: 'bar'}, 7 | 'bar', 8 | ]); 9 | expectAssignable>(aggregateError.errors); 10 | expectType>(aggregateError.errors[Symbol.iterator]()); 11 | 12 | for (const error of aggregateError.errors) { 13 | expectType(error); 14 | } 15 | 16 | class CustomError extends Error { 17 | public foo: string; 18 | 19 | constructor(message: string) { 20 | super(message); 21 | this.name = 'CustomError'; 22 | this.foo = 'bar'; 23 | } 24 | } 25 | const customAggregateError = new AggregateError([ 26 | new CustomError('foo'), 27 | ]); 28 | 29 | for (const error of customAggregateError.errors) { 30 | expectType(error.foo); 31 | } 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aggregate-error", 3 | "version": "5.0.0", 4 | "description": "Create an error from multiple errors", 5 | "license": "MIT", 6 | "repository": "sindresorhus/aggregate-error", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "engines": { 19 | "node": ">=18" 20 | }, 21 | "scripts": { 22 | "test": "xo && ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts" 27 | ], 28 | "keywords": [ 29 | "aggregate", 30 | "error", 31 | "combine", 32 | "multiple", 33 | "many", 34 | "collection", 35 | "iterable", 36 | "iterator" 37 | ], 38 | "dependencies": { 39 | "clean-stack": "^5.2.0", 40 | "indent-string": "^5.0.0" 41 | }, 42 | "devDependencies": { 43 | "ava": "^5.3.1", 44 | "tsd": "^0.29.0", 45 | "xo": "^0.56.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import indentString from 'indent-string'; 2 | import cleanStack from 'clean-stack'; 3 | 4 | const cleanInternalStack = stack => stack.replaceAll(/\s+at .*aggregate-error\/index.js:\d+:\d+\)?/g, ''); 5 | 6 | export default class AggregateError extends Error { 7 | #errors; 8 | 9 | name = 'AggregateError'; 10 | 11 | constructor(errors) { 12 | if (!Array.isArray(errors)) { 13 | throw new TypeError(`Expected input to be an Array, got ${typeof errors}`); 14 | } 15 | 16 | errors = errors.map(error => { 17 | if (error instanceof Error) { 18 | return error; 19 | } 20 | 21 | if (error !== null && typeof error === 'object') { 22 | // Handle plain error objects with message property and/or possibly other metadata 23 | return Object.assign(new Error(error.message), error); 24 | } 25 | 26 | return new Error(error); 27 | }); 28 | 29 | let message = errors 30 | .map(error => 31 | // The `stack` property is not standardized, so we can't assume it exists 32 | typeof error.stack === 'string' && error.stack.length > 0 ? cleanInternalStack(cleanStack(error.stack)) : String(error), 33 | ) 34 | .join('\n'); 35 | message = '\n' + indentString(message, 4); 36 | super(message); 37 | 38 | this.#errors = errors; 39 | } 40 | 41 | get errors() { 42 | return [...this.#errors]; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Create an error from multiple errors. 3 | */ 4 | export default class AggregateError extends Error { 5 | readonly name: 'AggregateError'; 6 | 7 | readonly errors: readonly [T]; 8 | 9 | /** 10 | @param errors - If a string, a new `Error` is created with the string as the error message. If a non-Error object, a new `Error` is created with all properties from the object copied over. 11 | 12 | @example 13 | ``` 14 | import AggregateError from 'aggregate-error'; 15 | 16 | const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]); 17 | 18 | throw error; 19 | 20 | // AggregateError: 21 | // Error: foo 22 | // at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:33) 23 | // Error: bar 24 | // at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) 25 | // Error: baz 26 | // at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) 27 | // at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3) 28 | // at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) 29 | // at Module._compile (module.js:556:32) 30 | // at Object.Module._extensions..js (module.js:565:10) 31 | // at Module.load (module.js:473:32) 32 | // at tryModuleLoad (module.js:432:12) 33 | // at Function.Module._load (module.js:424:3) 34 | // at Module.runMain (module.js:590:10) 35 | // at run (bootstrap_node.js:394:7) 36 | // at startup (bootstrap_node.js:149:9) 37 | 38 | for (const individualError of error.errors) { 39 | console.log(individualError); 40 | } 41 | //=> [Error: foo] 42 | //=> [Error: bar] 43 | //=> [Error: baz] 44 | ``` 45 | */ 46 | constructor(errors: ReadonlyArray | string>); 47 | } 48 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import AggregateError from './index.js'; 3 | 4 | test('main', t => { 5 | const error = new AggregateError([ 6 | new Error('foo'), 7 | 'bar', 8 | { 9 | message: 'baz', 10 | code: 'EBAZ', 11 | }, 12 | { 13 | code: 'EQUX', 14 | }, 15 | ]); 16 | 17 | console.log(error); 18 | 19 | t.regex(error.message, /Error: foo\n {8}at /); 20 | t.regex(error.message, /Error: bar\n {8}at /); 21 | 22 | t.deepEqual([...error.errors], [ 23 | new Error('foo'), 24 | new Error('bar'), 25 | Object.assign(new Error('baz'), {code: 'EBAZ'}), 26 | Object.assign(new Error(), {code: 'EQUX'}), // eslint-disable-line unicorn/error-message 27 | ]); 28 | }); 29 | 30 | test('gracefully handle Error instances without a stack', t => { 31 | class StacklessError extends Error { 32 | constructor(...args) { 33 | super(...args); 34 | this.name = this.constructor.name; 35 | delete this.stack; 36 | } 37 | } 38 | 39 | const error = new AggregateError([ 40 | new Error('foo'), 41 | new StacklessError('stackless'), 42 | ]); 43 | 44 | console.log(error); 45 | 46 | t.regex(error.message, /Error: foo\n {8}at /); 47 | t.regex(error.message, /StacklessError: stackless/); 48 | 49 | t.deepEqual([...error.errors], [ 50 | new Error('foo'), 51 | new StacklessError('stackless'), 52 | ]); 53 | }); 54 | 55 | test('gracefully handle Error instances with empty stack', t => { 56 | class EmptyStackError extends Error { 57 | constructor(...args) { 58 | super(...args); 59 | this.name = this.constructor.name; 60 | this.stack = ''; 61 | } 62 | } 63 | 64 | const error = new AggregateError([ 65 | new Error('foo'), 66 | new EmptyStackError('emptystack'), 67 | ]); 68 | 69 | console.log(error); 70 | 71 | t.regex(error.message, /Error: foo\n {8}at /); 72 | t.regex(error.message, /EmptyStackError: emptystack/); 73 | 74 | t.deepEqual([...error.errors], [ 75 | new Error('foo'), 76 | new EmptyStackError('emptystack'), 77 | ]); 78 | }); 79 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # aggregate-error 2 | 3 | > Create an error from multiple errors 4 | 5 | *Note: With [Node.js 15](https://medium.com/@nodejs/node-js-v15-0-0-is-here-deb00750f278), there's now a built-in [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) type.* 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install aggregate-error 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import AggregateError from 'aggregate-error'; 17 | 18 | const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]); 19 | 20 | throw error; 21 | /* 22 | AggregateError: 23 | Error: foo 24 | at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:33) 25 | Error: bar 26 | at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) 27 | Error: baz 28 | at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) 29 | at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3) 30 | at Object. (/Users/sindresorhus/dev/aggregate-error/example.js:3:13) 31 | at Module._compile (module.js:556:32) 32 | at Object.Module._extensions..js (module.js:565:10) 33 | at Module.load (module.js:473:32) 34 | at tryModuleLoad (module.js:432:12) 35 | at Function.Module._load (module.js:424:3) 36 | at Module.runMain (module.js:590:10) 37 | at run (bootstrap_node.js:394:7) 38 | at startup (bootstrap_node.js:149:9) 39 | */ 40 | 41 | for (const individualError of error.errors) { 42 | console.log(individualError); 43 | } 44 | //=> [Error: foo] 45 | //=> [Error: bar] 46 | //=> [Error: baz] 47 | ``` 48 | 49 | ## API 50 | 51 | ### AggregateError(errors) 52 | 53 | Returns an `Error`. 54 | 55 | #### errors 56 | 57 | Type: `Array` 58 | 59 | If a string, a new `Error` is created with the string as the error message.\ 60 | If a non-Error object, a new `Error` is created with all properties from the object copied over. 61 | --------------------------------------------------------------------------------