This content originally appeared on David Walsh Blog and was authored by David Walsh
One of the big themes of the web these days is concurrency, which leads to accomplishing tasks asynchronously. In doing so, the possibility of multiple errors can occur. Instead of providing a generic error, optimally you’d provide a wealth of error information. TheAggregateError
error lets developers throw multiple errors within one single Error
. Let’s see how it works.
To throw a single error that represents multiple errors, let’s employ AggregateError
:
const error = new AggregateError([ new Error('ERROR_11112'), new TypeError('First name must be a string'), new RangeError('Transaction value must be at least 1'), new URIError('User profile link must be https'), ], 'Transaction cannot be processed')
Throwing an AggregateError
gets you the following information:
error instanceof AggregateError // true error.name // 'AggregateError' error.message // 'Transaction cannot be processed' error.errors // The array of errors
The AggregateError
is incredibly useful when validating multiple sets of data; instead of throwing one error at a time, grouping them into one is ideal! AggregateError
would be really useful in a Promise.any
situation. Communicative, information-rich errors FTW!
The post AggregateError appeared first on David Walsh Blog.
This content originally appeared on David Walsh Blog and was authored by David Walsh
David Walsh | Sciencx (2021-09-17T15:12:17+00:00) AggregateError. Retrieved from https://www.scien.cx/2021/09/17/aggregateerror/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.