Comparing version 1.2.6 to 1.2.7
@@ -74,5 +74,10 @@ "use strict"; | ||
const C = this.getClass(highLvErrName); | ||
return this.reformatTrace(new C(this.getHighLevelError(highLvErrName), { | ||
const err = new C(this.getHighLevelError(highLvErrName), { | ||
message | ||
}, this._config.baseErrorConfig)); | ||
}, this._config.baseErrorConfig); | ||
if (typeof this._config.onCreateError === 'function') { | ||
this._config.onCreateError(err); | ||
} | ||
this.reformatTrace(err); | ||
return err; | ||
} | ||
@@ -90,3 +95,8 @@ /** | ||
const C = this.getClass(highLvErrName); | ||
return this.reformatTrace(new C(this.getHighLevelError(highLvErrName), this.getLowLevelError(lowLvErrName), this._config.baseErrorConfig)); | ||
const err = new C(this.getHighLevelError(highLvErrName), this.getLowLevelError(lowLvErrName), this._config.baseErrorConfig); | ||
if (typeof this._config.onCreateError === 'function') { | ||
this._config.onCreateError(err); | ||
} | ||
this.reformatTrace(err); | ||
return err; | ||
} | ||
@@ -101,3 +111,2 @@ /** | ||
err.stack = stack.join('\n'); | ||
return err; | ||
} | ||
@@ -104,0 +113,0 @@ /** |
/** | ||
* A High Level Error definition defined by the user | ||
*/ | ||
import { BaseRegistryError } from './error-types/BaseRegistryError'; | ||
export interface HighLevelError { | ||
@@ -234,2 +235,6 @@ /** | ||
baseErrorConfig?: IBaseErrorConfig; | ||
/** | ||
* Handler to modify the created error when newError / newBareError is called | ||
*/ | ||
onCreateError?: (err: BaseRegistryError) => void; | ||
} | ||
@@ -236,0 +241,0 @@ /** |
"use strict"; | ||
/** | ||
* A High Level Error definition defined by the user | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=interfaces.js.map |
@@ -0,1 +1,9 @@ | ||
## 1.2.7 - Tue Mar 09 2021 01:55:52 | ||
**Contributor:** Theo Gravity | ||
- Add `ErrorRegistry` config option `onCreateError` | ||
You can now globally modify new errors created from the error registry via the `onCreateError` handler. | ||
## 1.2.6 - Tue Mar 09 2021 00:23:04 | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "new-error", | ||
"version": "1.2.6", | ||
"version": "1.2.7", | ||
"description": "A production-grade error creation and serialization library designed for Typescript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -402,2 +402,6 @@ # new-error | ||
baseErrorConfig?: IBaseErrorConfig | ||
/** | ||
* Handler to modify the created error when newError / newBareError is called | ||
*/ | ||
onCreateError?: (err: BaseRegistryError) => void | ||
} | ||
@@ -448,2 +452,19 @@ ``` | ||
### Error creation handler | ||
If you want all errors created from the registry to have defined properties, you can use the `onCreateError` config option to modify the created error. | ||
For example, if you want to create an error id for each new error: | ||
```ts | ||
const errRegistry = new ErrorRegistry(errors, errorCodes, { | ||
onCreateError: (err) => { | ||
err.withErrorId('test-id') | ||
} | ||
}) | ||
// the err should have 'test-id' set for the error id | ||
const err = errRegistry.newError('INTERNAL_SERVER_ERROR', 'DATABASE_FAILURE') | ||
``` | ||
## `instanceOf` / comparisons | ||
@@ -450,0 +471,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84215
1088
937