1'use strict'; 2const indentString = require('indent-string'); 3const cleanStack = require('clean-stack'); 4 5const cleanInternalStack = stack => stack.replace(/\s+at .*aggregate-error\/index.js:\d+:\d+\)?/g, ''); 6 7class AggregateError extends Error { 8 constructor(errors) { 9 if (!Array.isArray(errors)) { 10 throw new TypeError(`Expected input to be an Array, got ${typeof errors}`); 11 } 12 13 errors = [...errors].map(error => { 14 if (error instanceof Error) { 15 return error; 16 } 17 18 if (error !== null && typeof error === 'object') { 19 // Handle plain error objects with message property and/or possibly other metadata 20 return Object.assign(new Error(error.message), error); 21 } 22 23 return new Error(error); 24 }); 25 26 let message = errors 27 .map(error => { 28 // The `stack` property is not standardized, so we can't assume it exists 29 return typeof error.stack === 'string' ? cleanInternalStack(cleanStack(error.stack)) : String(error); 30 }) 31 .join('\n'); 32 message = '\n' + indentString(message, 4); 33 super(message); 34 35 this.name = 'AggregateError'; 36 37 Object.defineProperty(this, '_errors', {value: errors}); 38 } 39 40 * [Symbol.iterator]() { 41 for (const error of this._errors) { 42 yield error; 43 } 44 } 45} 46 47module.exports = AggregateError; 48