error-custom-class
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -79,2 +79,3 @@ /** | ||
onCreate?: OnCreate<ErrorNameArg, ErrorParamsArg>, | ||
ParentClass?: Function, | ||
): typeof CustomError<ErrorNameArg, ErrorParamsArg> |
@@ -13,4 +13,8 @@ import{setErrorName}from"./name.js"; | ||
export default function errorCustomClass(name,onCreate=defaultOnCreate){ | ||
const CustomErrorClass=class extends Error{ | ||
export default function errorCustomClass( | ||
name, | ||
onCreate=defaultOnCreate, | ||
ParentClass=Error) | ||
{ | ||
const CustomErrorClass=class extends ParentClass{ | ||
constructor(message,params){ | ||
@@ -17,0 +21,0 @@ super(message,getErrorParams(params)); |
{ | ||
"name": "error-custom-class", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"type": "module", | ||
@@ -49,3 +49,3 @@ "exports": "./build/src/main.js", | ||
"devDependencies": { | ||
"@ehmicky/dev-tasks": "^1.0.85", | ||
"@ehmicky/dev-tasks": "^1.0.86", | ||
"error-cause": "^1.0.4", | ||
@@ -52,0 +52,0 @@ "error-cause-polyfill": "^1.1.0", |
@@ -19,3 +19,4 @@ [![Codecov](https://img.shields.io/codecov/c/github/ehmicky/error-custom-class.svg?label=tested&logo=codecov)](https://codecov.io/gh/ehmicky/error-custom-class) | ||
[older Node.js and browsers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause#browser_compatibility) | ||
- Optional [custom initialization logic](#custom-initialization-logic) | ||
- Optional [parent class](#parent-class) or | ||
[custom initialization logic](#custom-initialization-logic) | ||
@@ -58,2 +59,14 @@ # Examples | ||
## Parent class | ||
<!-- eslint-disable promise/prefer-await-to-callbacks --> | ||
```js | ||
const ParentError = errorCustomClass('ParentError') | ||
const ChildError = errorCustomClass('ChildError', onCreate, ParentError) | ||
const childError = new ChildError('message') | ||
console.log(childError instanceof ChildError) // true | ||
console.log(childError instanceof ParentError) // true | ||
``` | ||
## Custom initialization logic | ||
@@ -84,6 +97,7 @@ | ||
## errorCustomClass(errorName, onCreate?) | ||
## errorCustomClass(errorName, onCreate?, ParentClass?) | ||
`errorName` `string`\ | ||
`onCreate` `(error, params) => void`\ | ||
`ParentClass` `typeof Error`\ | ||
_Return value_: `CustomError` | ||
@@ -149,2 +163,8 @@ | ||
error with its `cause` | ||
- [`set-error-class`](https://github.com/ehmicky/set-error-class): Properly | ||
update an error's class | ||
- [`set-error-message`](https://github.com/ehmicky/set-error-message): Properly | ||
update an error's message | ||
- [`set-error-props`](https://github.com/ehmicky/set-error-props): Properly | ||
update an error's properties | ||
- [`error-cause-polyfill`](https://github.com/ehmicky/error-cause-polyfill): | ||
@@ -151,0 +171,0 @@ Polyfill `error.cause` |
26052
200
200