-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]) |