Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent d7eb866 commit 3db449b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
44 changes: 44 additions & 0 deletions src/error/merge/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Ensure both the prototype and `error.name` are correct, by creating a new
// instance with the right constructor.
// The parent error's type has priority unless:
// - It is Error|AggregateError, which allows wrapping an error message or
// properties without changing its type
// - Its constructor throws
// If both parent and child constructors throw, we default to Error.
// If both parent and child are Error|AggregateError, we privilege
// AggregateError if any is an instance, since the `errors` property should only
// be used on AggregateError.
export const createError = function (parent, child, message) {
if (isSimpleErrorType(parent)) {
return createCauseError(parent, child, message)
}

try {
return new parent.constructor(message)
} catch {
return createCauseError(parent, child, message)
}
}

const createCauseError = function (parent, child, message) {
if (isSimpleErrorType(child)) {
return createSimpleErrorType(parent, child, message)
}

try {
return new child.constructor(message)
} catch {
return createSimpleErrorType(parent, child, message)
}
}

const isSimpleErrorType = function (error) {
return error.constructor === Error || error.constructor === AggregateError
}

const createSimpleErrorType = function (parent, child, message) {
return parent.constructor === AggregateError ||
child.constructor === AggregateError
? new AggregateError([], message)
: new Error(message)
}
46 changes: 1 addition & 45 deletions src/error/merge/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { normalizeError } from '../normalize/main.js'
import { setErrorProperty } from '../normalize/set.js'

import { createError } from './create.js'
import { mergeMessage } from './message.js'
import { copyProps } from './props.js'
import { hasStack, fixStack } from './stack.js'
Expand Down Expand Up @@ -59,51 +60,6 @@ const mergeCause = function (parent, parentErrors, hasChildStack) {
return normalizeError(mergedError)
}

// Ensure both the prototype and `error.name` are correct, by creating a new
// instance with the right constructor.
// The parent error's type has priority unless:
// - It is Error|AggregateError, which allows wrapping an error message or
// properties without changing its type
// - Its constructor throws
// If both parent and child constructors throw, we default to Error.
// If both parent and child are Error|AggregateError, we privilege
// AggregateError if any is an instance, since the `errors` property should only
// be used on AggregateError.
const createError = function (parent, child, message) {
if (isSimpleErrorType(parent)) {
return createCauseError(parent, child, message)
}

try {
return new parent.constructor(message)
} catch {
return createCauseError(parent, child, message)
}
}

const createCauseError = function (parent, child, message) {
if (isSimpleErrorType(child)) {
return createSimpleErrorType(parent, child, message)
}

try {
return new child.constructor(message)
} catch {
return createSimpleErrorType(parent, child, message)
}
}

const isSimpleErrorType = function (error) {
return error.constructor === Error || error.constructor === AggregateError
}

const createSimpleErrorType = function (parent, child, message) {
return parent.constructor === AggregateError ||
child.constructor === AggregateError
? new AggregateError([], message)
: new Error(message)
}

// Keep `error.errors` when merging errors.
// If multiple errors have `errors`, the parent's errors are prepended.
// `error.errors[*].cause` are recursed.
Expand Down

0 comments on commit 3db449b

Please sign in to comment.