Error Handling in Async Code
What advanced error reporting pattern is shown?
class RetryError extends AggregateError {
constructor(errors, message, attempts) {
super(errors, message);
this.attempts = attempts;
}
}
async function withRetry(fn, maxAttempts = 3) {
const errors = [];
for (let i = 0; i < maxAttempts; i++) {
try {
return await fn(i);
} catch (error) {
errors.push(error);
if (i === maxAttempts - 1) {
throw new RetryError(errors, 'All retry attempts failed', maxAttempts);
}
}
}
}