Skip to content

Commit

Permalink
Improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent c7728e0 commit 14e6448
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/error/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@ import { setErrorProperty } from './normalize/set.js'
/// which is very verbose and hard to read
// In Node <16.9.0 and in some browsers, `error.cause` requires a polyfill like
// `error-cause`.
export const mergeErrorCause = function (error) {
const errorA = normalizeError(error)
return mergeCause(errorA)
}

// The recursion is applied pair by pair, as opposed to all errors at once.
// - This ensures the same result no matter how many times this function
// applied along the stack trace
// `normalizeError()` is called again to ensure the new `name|message` is
// reflected in `error.stack`.
const mergeCause = function (parent) {
export const mergeErrorCause = function (error) {
const parent = normalizeError(error)

if (parent.cause === undefined) {
return parent
}

const child = mergeCause(parent.cause)
const child = mergeErrorCause(parent.cause)
const message = mergeMessage(parent.message, child.message)
const mergedError = createError(parent, child, message)
fixStack(mergedError, child)
Expand Down Expand Up @@ -167,7 +164,9 @@ const mergeAggregate = function (mergedError, parent, child) {
}

const getAggregateErrors = function (error) {
return hasAggregateErrors(error) ? error.errors.map(mergeCause) : undefined
return hasAggregateErrors(error)
? error.errors.map(mergeErrorCause)
: undefined
}

const hasAggregateErrors = function (error) {
Expand Down

0 comments on commit 14e6448

Please sign in to comment.