Skip to content

Commit

Permalink
refactor: Split error handling helpers for promise utils (#30195)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 15, 2024
1 parent b7d051e commit ee3ad7f
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/util/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ function isExternalHostError(err: any): err is ExternalHostError {
return err instanceof ExternalHostError;
}

function handleError(err: any): never {
if (!(err instanceof AggregateError)) {
throw err;
}

logger.debug({ err }, 'Aggregate error is thrown');

const errors = [...err];

function handleMultipleErrors(errors: Error[]): never {
const hostError = errors.find(isExternalHostError);
if (hostError) {
throw hostError;
}

if (
errors.length === 1 ||
new Set(errors.map(({ message }) => message)).size === 1
Expand All @@ -32,7 +23,16 @@ function handleError(err: any): never {
throw error;
}

throw err;
throw new AggregateError(errors);
}

function handleError(err: any): never {
if (!(err instanceof AggregateError)) {
throw err;
}

logger.debug({ err }, 'Aggregate error is thrown');
handleMultipleErrors([...err]);
}

export async function all<T>(
Expand Down

0 comments on commit ee3ad7f

Please sign in to comment.