A few days ago, I was reviewing all the error types in JavaScript and came across something relatively new: AggregateError
Error Type | Description |
---|---|
Error | Base error object for generic errors. |
SyntaxError | Thrown when there is a syntax mistake in the code. |
ReferenceError | Thrown when referencing a variable that doesnβt exist. |
TypeError | Thrown when a value is not of the expected type. |
RangeError | Thrown when a number is outside an allowable range. |
URIError | Thrown when there is an issue with encoding/decoding URI components. |
EvalError | Historical, used for improper eval() usage (mostly obsolete). |
AggregateError | Thrown when multiple errors need to be handled together. (added in ECMAScript 2021) |
AggregateError
was added in ECMAScript 2021 (ES12). Itβs designed to help when you need to deal with multiple errors at once. This is super handy in scenarios like working with promises, where you might want to handle all errors together instead of one by one.
If you use Promise.any()
to find the first successful promise, but they all fail, JavaScript will throw an AggregateError
. This error lists all the reasons why the promises failed.
Breakdown
-
I use
Promise.any()
to try fetching data from multiple APIs, resolving with the first successful result. If all promises fail, it throws anAggregateError
. -
I handle the potential errors with a
try/catch
block, where thetry
block deals with successful data fetching, and thecatch
block catches theAggregateError
if all promises are rejected. -
If an
AggregateError
occurs, I log the error details and then attempt to retry the operation up to a specified number of times, with a delay between each retry. -
I implement the retry logic by recursively calling the function, reducing the retry count each time, until either I get a successful result or exhaust all retries.
-
If all retries fail, I log a final error message and rethrow the
AggregateError
so it can be handled further if needed.
AggregateError
simplifies error handling when youβre dealing with multiple failures at once, especially in asynchronous code. Itβs one of those features that might not seem necessary until you run into a situation where it saves you a ton of headaches.
Without AggregateError
To illustrate the usefulness of AggregateError
, Iβll show how handling multiple promise rejections without it can be cumbersome and less efficient. Hereβs how you might handle this situation without AggregateError
When Promise.any()
rejects, I donβt have an AggregateError
to easily access all the errors. Instead, I loop through the original promises and individually check which ones failed by attempting to await them again, logging each failure.
If you donβt use AggregateError
, you might have to throw
and handle each error individually, which complicates the error-handling logic.
Problems Without AggregateError
- Limited Feedback: The function stops at the first error, so the user only sees one error message at a time. This can be frustrating for users, as they have to fix one issue and submit the form again to see the next error.
- Cumbersome Code: Handling each error individually requires repetitive code if you want to collect and display all errors at once.
With AggregateError
WithΒ AggregateError
, error handling is centralized and simplified. It automatically collects all the rejections, allowing you to manage them in a more structured way by grouping multiple errors instead of dealing with each one separately.
Benefits of Using AggregateError
- Consolidated Error Reporting: All validation errors are collected and thrown together. The user gets feedback on all issues at once, which improves the user experience.
- Simpler Error Handling: Instead of handling multiple individual errors, you only need one
try/catch
block to handle them all. - Complete Debugging Information: The
AggregateError
contains a list of all the errors, making it easier to debug and understand what went wrong.
Using AggregateError
to group multiple errors has several advantages. Itβs especially helpful when multiple things can go wrong at once, like during form validation or parallel API requests.