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 3e44857 commit 1ca9850
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/error/merge/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { normalizeError } from '../normalize/main.js'
import { setErrorProperty } from '../normalize/set.js'

import { copyProps } from './props.js'

// Merge `error.cause` recursively to a single error.
// This allows consumers to conveniently wrap errors using:
// try {
Expand Down Expand Up @@ -212,43 +214,4 @@ const getMergedErrors = function (parentErrors, childErrors) {

return [...childErrors, ...parentErrors]
}

// Merge error properties, shallowly, with parent error having priority
const copyProps = function (mergedError, parent, child) {
mergeProps(mergedError, child)
mergeProps(mergedError, parent)
}

// Do not merge inherited properties nor non-enumerable properties.
// Works with symbol properties.
const mergeProps = function (mergedError, error) {
// eslint-disable-next-line guard-for-in, fp/no-loops
for (const propName in error) {
mergeProp(mergedError, error, propName)
}

// eslint-disable-next-line fp/no-loops
for (const propName of Object.getOwnPropertySymbols(error)) {
mergeProp(mergedError, error, propName)
}
}

const mergeProp = function (mergedError, error, propName) {
const descriptor = Object.getOwnPropertyDescriptor(error, propName)

if (descriptor !== undefined && !CORE_ERROR_PROPS.has(propName)) {
// eslint-disable-next-line fp/no-mutating-methods
Object.defineProperty(mergedError, propName, descriptor)
}
}

// Do not copy core error properties.
// Does not assume they are not enumerable.
const CORE_ERROR_PROPS = new Set([
'name',
'message',
'stack',
'cause',
'errors',
])
/* eslint-enable max-lines */
38 changes: 38 additions & 0 deletions src/error/merge/props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Merge error properties, shallowly, with parent error having priority
export const copyProps = function (mergedError, parent, child) {
mergeProps(mergedError, child)
mergeProps(mergedError, parent)
}

// Do not merge inherited properties nor non-enumerable properties.
// Works with symbol properties.
const mergeProps = function (mergedError, error) {
// eslint-disable-next-line guard-for-in, fp/no-loops
for (const propName in error) {
mergeProp(mergedError, error, propName)
}

// eslint-disable-next-line fp/no-loops
for (const propName of Object.getOwnPropertySymbols(error)) {
mergeProp(mergedError, error, propName)
}
}

const mergeProp = function (mergedError, error, propName) {
const descriptor = Object.getOwnPropertyDescriptor(error, propName)

if (descriptor !== undefined && !CORE_ERROR_PROPS.has(propName)) {
// eslint-disable-next-line fp/no-mutating-methods
Object.defineProperty(mergedError, propName, descriptor)
}
}

// Do not copy core error properties.
// Does not assume they are not enumerable.
const CORE_ERROR_PROPS = new Set([
'name',
'message',
'stack',
'cause',
'errors',
])

0 comments on commit 1ca9850

Please sign in to comment.