Comparing version 1.2.2 to 1.2.3
@@ -195,3 +195,3 @@ "use strict"; | ||
var _a; | ||
const data = { | ||
let data = { | ||
errId: this._errId, | ||
@@ -217,2 +217,5 @@ name: this.name, | ||
} | ||
if (this._config.onPreToJSONData) { | ||
data = this._config.onPreToJSONData(data); | ||
} | ||
Object.keys(data).forEach(item => { | ||
@@ -239,3 +242,3 @@ // remove undefined items | ||
var _a; | ||
const data = { | ||
let data = { | ||
errId: this._errId, | ||
@@ -258,2 +261,5 @@ code: this._code, | ||
}); | ||
if (this._config.onPreToJSONSafeData) { | ||
data = this._config.onPreToJSONSafeData(data); | ||
} | ||
fieldsToOmit.forEach(item => { | ||
@@ -260,0 +266,0 @@ delete data[item]; |
@@ -215,2 +215,12 @@ /** | ||
omitEmptyMetadata?: boolean; | ||
/** | ||
* A function to run against the computed data when calling `toJSON`. This is called prior | ||
* to field omission. If defined, must return the data back. | ||
*/ | ||
onPreToJSONData?: (data: Partial<SerializedError>) => Partial<SerializedError>; | ||
/** | ||
* A function to run against the computed safe data when calling `toJSONSafe`. This is called | ||
* prior to field omission. If defined, must return the data back. | ||
*/ | ||
onPreToJSONSafeData?: (data: Partial<SerializedErrorSafe>) => Partial<SerializedErrorSafe>; | ||
} | ||
@@ -252,5 +262,9 @@ /** | ||
/** | ||
* User-defined metadata | ||
* User-defined metadata. May not be present if there is no data and omitEmptyMetadata is enabled | ||
*/ | ||
meta: Record<string, any>; | ||
meta?: Record<string, any>; | ||
/** | ||
* Other optional values | ||
*/ | ||
[key: string]: any; | ||
} | ||
@@ -257,0 +271,0 @@ /** |
@@ -0,1 +1,9 @@ | ||
## 1.2.3 - Mon Mar 08 2021 21:59:38 | ||
**Contributor:** Theo Gravity | ||
- Add toJSON / toJSONSafe post-processing handler options (#10) | ||
You can now perform post-processing on serialized data via `onPreToJSONData` / `onPreToJSONDataSafe` options. See readme for more details. | ||
## 1.2.2 - Mon Mar 08 2021 21:06:07 | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "new-error", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "A production-grade error creation and serialization library designed for Typescript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -58,2 +58,3 @@ # new-error | ||
- [Internal serialization](#internal-serialization) | ||
- [Post-processing handlers](#post-processing-handlers) | ||
- [Deserialization](#deserialization) | ||
@@ -494,2 +495,12 @@ - [Issues with deserialization](#issues-with-deserialization) | ||
omitEmptyMetadata?: boolean | ||
/** | ||
* A function to run against the computed data when calling `toJSON`. This is called prior | ||
* to field omission. If defined, must return the data back. | ||
*/ | ||
onPreToJSONData?: (data: Partial<SerializedError>) => Partial<SerializedError> | ||
/** | ||
* A function to run against the computed safe data when calling `toJSONSafe`. This is called | ||
* prior to field omission. If defined, must return the data back. | ||
*/ | ||
onPreToJSONSafeData?: (data: Partial<SerializedErrorSafe>) => Partial<SerializedErrorSafe> | ||
} | ||
@@ -710,2 +721,32 @@ ``` | ||
### Post-processing handlers | ||
The `BaseError` config `onPreToJSONData` / `onPreToJSONSafeData` options allow post-processing of the data. This is useful if you want to decorate your data for all new | ||
errors created. | ||
```ts | ||
const errRegistry = new ErrorRegistry(errors, errorCodes, { | ||
// called when toJSON is called | ||
onPreToJSONData: (data) => { | ||
// we want all new errors to contain a date field | ||
data.date = new Date().tostring() | ||
// add some additional metadata | ||
// data.meta might be empty if omitEmptyMetadata is enabled | ||
if (data.meta) { | ||
data.meta.moreData = 'test' | ||
} | ||
return data | ||
} | ||
}) | ||
const err = errRegistry.newError('INTERNAL_SERVER_ERROR', 'DATABASE_FAILURE') | ||
.setErrorId('err-1234') | ||
.formatMessage('SQL_1234') | ||
// should produce the standard error structure, but with the new fields added | ||
console.log(err.toJSON()) | ||
``` | ||
# Deserialization | ||
@@ -712,0 +753,0 @@ |
Sorry, the diff of this file is not supported yet
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
80665
1062
902
83