merge-error-cause
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -14,42 +14,22 @@ import{setErrorProperty}from"./set.js"; | ||
export const mergeAggregate=function({ | ||
mergedError, | ||
parentErrors, | ||
child, | ||
mergeErrorCause}) | ||
{ | ||
const childErrors=getAggregateErrors(child,mergeErrorCause); | ||
if(parentErrors===undefined&&childErrors===undefined){ | ||
export const mergeAggregateCauses=function(parent,mergeErrorCause){ | ||
if(parent.errors===undefined){ | ||
return; | ||
} | ||
const errors=getMergedErrors(parentErrors,childErrors); | ||
setErrorProperty(mergedError,"errors",errors); | ||
const errors=parent.errors.map(mergeErrorCause); | ||
setErrorProperty(parent,"errors",errors); | ||
}; | ||
export const getAggregateErrors=function(error,mergeErrorCause){ | ||
return Array.isArray(error.errors)? | ||
error.errors.map(mergeErrorCause): | ||
undefined; | ||
}; | ||
const getMergedErrors=function(parentErrors,childErrors){ | ||
if(parentErrors===undefined){ | ||
return childErrors; | ||
export const mergeAggregateErrors=function(parent,child){ | ||
if(child.errors===undefined){ | ||
return; | ||
} | ||
if(childErrors===undefined){ | ||
return parentErrors; | ||
} | ||
return[...childErrors,...parentErrors]; | ||
const errors= | ||
parent.errors===undefined? | ||
child.errors: | ||
[...child.errors,...parent.errors]; | ||
setErrorProperty(parent,"errors",errors); | ||
}; | ||
export const setAggregate=function(parent,parentErrors){ | ||
if(parentErrors!==undefined){ | ||
setErrorProperty(parent,"errors",parentErrors); | ||
} | ||
}; | ||
//# sourceMappingURL=aggregate.js.map |
import normalizeException from"normalize-exception"; | ||
import{ | ||
setAggregate, | ||
getAggregateErrors, | ||
mergeAggregate}from | ||
"./aggregate.js"; | ||
import{createError}from"./create.js"; | ||
import{mergeAggregateCauses,mergeAggregateErrors}from"./aggregate.js"; | ||
import{mergeMessage}from"./message.js"; | ||
import{copyProps}from"./props.js"; | ||
import{mergePrototype}from"./prototype.js"; | ||
import{getStackIndex,fixStack}from"./stack.js"; | ||
@@ -28,23 +24,27 @@ | ||
const parent=normalizeException(error); | ||
const parentErrors=getAggregateErrors(parent,mergeErrorCause); | ||
mergeAggregateCauses(parent,mergeErrorCause); | ||
return mergeCause(parent,stackIndex); | ||
}; | ||
const mergeCause=function(parent,stackIndex){ | ||
if(parent.cause===undefined){ | ||
setAggregate(parent,parentErrors); | ||
return parent; | ||
} | ||
return mergeCause(parent,parentErrors,stackIndex); | ||
const child=mergeError(parent.cause,stackIndex-1); | ||
delete parent.cause; | ||
mergeChild(parent,child,stackIndex); | ||
return normalizeException(parent); | ||
}; | ||
const mergeCause=function(parent,parentErrors,stackIndex){ | ||
const child=mergeError(parent.cause,stackIndex-1); | ||
const message=mergeMessage(parent.message,child.message); | ||
const mergedError=createError(parent,child,message); | ||
fixStack({mergedError,parent,child,stackIndex}); | ||
mergeAggregate({mergedError,parentErrors,child,mergeErrorCause}); | ||
copyProps(mergedError,parent,child); | ||
return normalizeException(mergedError); | ||
const mergeChild=function(parent,child,stackIndex){ | ||
mergeMessage(parent,child); | ||
fixStack(parent,child,stackIndex); | ||
mergeAggregateErrors(parent,child); | ||
copyProps(parent,child); | ||
mergePrototype(parent,child); | ||
}; | ||
//# sourceMappingURL=main.js.map |
@@ -0,1 +1,2 @@ | ||
import{setErrorProperty}from"./set.js"; | ||
@@ -10,3 +11,9 @@ | ||
export const mergeMessage=function(rawParentMessage,rawChildMessage){ | ||
export const mergeMessage=function(parent,child){ | ||
const message=getMessage(parent.message,child.message); | ||
setErrorProperty(parent,"message",message); | ||
}; | ||
const getMessage=function(rawParentMessage,rawChildMessage){ | ||
const parentMessage=rawParentMessage.trim(); | ||
@@ -13,0 +20,0 @@ const childMessage=rawChildMessage.trim(); |
export const copyProps=function(mergedError,parent,child){ | ||
mergeProps(mergedError,child); | ||
mergeProps(mergedError,parent); | ||
}; | ||
export const copyProps=function(parent,child){ | ||
const mergeProps=function(mergedError,error){ | ||
for(const propName of Reflect.ownKeys(error)){ | ||
mergeProp(mergedError,error,propName); | ||
for(const propName of Reflect.ownKeys(child)){ | ||
mergeProp(parent,child,propName); | ||
} | ||
}; | ||
const mergeProp=function(mergedError,error,propName){ | ||
const descriptor=Object.getOwnPropertyDescriptor(error,propName); | ||
const mergeProp=function(parent,child,propName){ | ||
if(propName in parent){ | ||
return; | ||
} | ||
const descriptor=Object.getOwnPropertyDescriptor(child,propName); | ||
if(descriptor!==undefined&&!CORE_ERROR_PROPS.has(propName)){ | ||
Object.defineProperty(mergedError,propName,descriptor); | ||
Object.defineProperty(parent,propName,descriptor); | ||
} | ||
@@ -23,0 +22,0 @@ }; |
@@ -43,6 +43,7 @@ import{setErrorProperty}from"./set.js"; | ||
export const fixStack=function({mergedError,parent,child,stackIndex}){ | ||
const{stack}=stackIndex>0?child:parent; | ||
setErrorProperty(mergedError,"stack",stack); | ||
export const fixStack=function(parent,child,stackIndex){ | ||
if(stackIndex>0){ | ||
setErrorProperty(parent,"stack",child.stack); | ||
} | ||
}; | ||
//# sourceMappingURL=stack.js.map |
{ | ||
"name": "merge-error-cause", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"type": "module", | ||
@@ -49,8 +49,7 @@ "exports": "./build/src/main.js", | ||
"dependencies": { | ||
"normalize-exception": "^1.5.0" | ||
"normalize-exception": "^1.8.0" | ||
}, | ||
"devDependencies": { | ||
"@ehmicky/dev-tasks": "^1.0.84", | ||
"error-type": "^1.3.0", | ||
"test-each": "^5.2.0" | ||
"test-each": "^5.2.1" | ||
}, | ||
@@ -57,0 +56,0 @@ "engines": { |
[![Codecov](https://img.shields.io/codecov/c/github/ehmicky/merge-error-cause.svg?label=tested&logo=codecov)](https://codecov.io/gh/ehmicky/merge-error-cause) | ||
[![Node](https://img.shields.io/node/v/merge-error-cause.svg?logo=node.js)](https://www.npmjs.com/package/merge-error-cause) | ||
[![TypeScript](https://img.shields.io/badge/-typed-brightgreen?logo=typescript&colorA=gray)](/src/main.d.ts) | ||
[![TypeScript](https://img.shields.io/badge/-typed-brightgreen?logo=typescript&colorA=gray&logoColor=0096ff)](/src/main.d.ts) | ||
[![Node](https://img.shields.io/node/v/merge-error-cause.svg?logo=node.js&logoColor=66cc33)](https://www.npmjs.com/package/merge-error-cause) | ||
[![Twitter](https://img.shields.io/badge/%E2%80%8B-twitter-brightgreen.svg?logo=twitter)](https://twitter.com/intent/follow?screen_name=ehmicky) | ||
@@ -69,7 +69,5 @@ [![Medium](https://img.shields.io/badge/%E2%80%8B-medium-brightgreen.svg?logo=medium)](https://medium.com/@ehmicky) | ||
This never throws. | ||
If `error` is an `Error` instance, it is modified then returned. Otherwise, a | ||
new `error` is created then returned. This never throws. | ||
If `error` is an `Error` instance and has a `cause`, `error` is modified then | ||
returned. Otherwise, a new `error` is created and returned. | ||
# Background | ||
@@ -121,4 +119,4 @@ | ||
- `error.cause.cause` might also exist (and so on) | ||
- If `error` is not an `Error` instance, `error.name` will throw | ||
- Recursing over `error.cause` might be infinite | ||
- If `error` is not an `Error` instance, `error.name` might throw | ||
- Recursing over `error.cause` might be an infinite cycle | ||
@@ -138,5 +136,7 @@ ### Solution | ||
if (error.code === 'E101') { | ||
/* ... */ | ||
} | ||
if (error.name === 'UserError') { | ||
/* ... */ | ||
} | ||
@@ -203,3 +203,3 @@ } | ||
``` | ||
UserError: User "15" does not exist. | ||
TypeError: User "15" does not exist. | ||
Invalid user. | ||
@@ -231,3 +231,3 @@ Could not create user. | ||
Child error messages are printed first. | ||
Inner error messages are printed first. | ||
@@ -245,3 +245,3 @@ ```js | ||
If the parent error message ends with `:`, it is prepended instead. | ||
If the outer error message ends with `:`, it is prepended instead. | ||
@@ -273,3 +273,3 @@ ```js | ||
By default, the parent error type is used. | ||
The outer error type is used. | ||
@@ -287,5 +287,4 @@ ```js | ||
If the parent error type is `Error` or `AggregateError`, the child type is used | ||
instead. This allows wrapping the error message or properties while keeping its | ||
type. | ||
If the parent error type is `Error`, the child type is used instead. This allows | ||
wrapping the error message or properties while keeping its type. | ||
@@ -301,5 +300,14 @@ ```js | ||
The error instance is created with `new ErrorType(message)`. If this throws, | ||
`new Error(message)` is used instead. | ||
`error.wrap: true` has the same effect, but works with any parent error type. | ||
```js | ||
try { | ||
throw new TypeError('User id is not a string.') | ||
} catch (cause) { | ||
const error = new AnyError('Could not create user.', { cause }) | ||
error.wrap = true | ||
console.log(mergeErrorCause(error) instanceof TypeError) // true | ||
} | ||
``` | ||
## Error properties | ||
@@ -309,15 +317,12 @@ | ||
<!-- eslint-disable fp/no-mutation --> | ||
<!-- eslint-disable fp/no-mutating-assign --> | ||
```js | ||
// Both `userId` and `invalidUser` are kept | ||
try { | ||
const error = new Error('Invalid user id.') | ||
// This is kept | ||
error.userId = '5' | ||
throw error | ||
throw Object.assign(new Error('Invalid user id.'), { userId: '5' }) | ||
} catch (cause) { | ||
const parentError = new Error('Could not create user.', { cause }) | ||
// This is kept too | ||
parentError.invalidUser = true | ||
throw parentError | ||
throw Object.assign(new Error('Could not create user.', { cause }), { | ||
invalidUser: true, | ||
}) | ||
} | ||
@@ -328,3 +333,3 @@ ``` | ||
<!-- eslint-disable fp/no-mutation, unicorn/error-message --> | ||
<!-- eslint-disable fp/no-mutating-assign, unicorn/error-message --> | ||
@@ -335,5 +340,3 @@ ```js | ||
} catch (cause) { | ||
const parentError = new Error('', { cause }) | ||
parentError.invalidUser = true | ||
throw parentError | ||
throw Object.assign(new Error('', { cause }), { invalidUser: true }) | ||
} | ||
@@ -370,4 +373,5 @@ ``` | ||
like it's 2022 🔮 | ||
- [`error-type`](https://github.com/ehmicky/error-type): Create custom error | ||
types | ||
- [`create-error-types`](https://github.com/ehmicky/create-error-types): Create | ||
multiple error types | ||
- [`error-type`](https://github.com/ehmicky/error-type): Create one error type | ||
- [`error-serializer`](https://github.com/ehmicky/error-serializer): Convert | ||
@@ -381,2 +385,4 @@ errors to/from plain objects | ||
handler for CLI applications 💥 | ||
- [`log-process-errors`](https://github.com/ehmicky/log-process-errors): Show | ||
some ❤ to Node.js process errors | ||
@@ -383,0 +389,0 @@ # Support |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
402
31121
196
Updatednormalize-exception@^1.8.0