error-serializer
Advanced tools
Comparing version 4.2.0 to 5.0.0
@@ -18,5 +18,5 @@ import isErrorInstance from"is-error-instance"; | ||
export const serialize=function(value,options){ | ||
const{normalize,shallow,beforeSerialize,afterSerialize}= | ||
const{loose,shallow,beforeSerialize,afterSerialize}= | ||
normalizeOptions(options); | ||
const valueA=applyNormalize(value,normalize); | ||
const valueA=applyLoose(value,loose); | ||
const events={beforeSerialize,afterSerialize}; | ||
@@ -35,3 +35,3 @@ return shallow? | ||
export const parse=function(value,options){ | ||
const{normalize,shallow,beforeParse,afterParse,classes}= | ||
const{loose,shallow,beforeParse,afterParse,classes}= | ||
normalizeOptions(options); | ||
@@ -42,10 +42,8 @@ const events={beforeParse,afterParse}; | ||
parseDeep(value,events,classes); | ||
return applyNormalize(valueA,normalize); | ||
return applyLoose(valueA,loose); | ||
}; | ||
const applyNormalize=function(value,normalize){ | ||
return normalize&&!isErrorInstance(value)? | ||
normalizeException(value): | ||
value; | ||
const applyLoose=function(value,loose){ | ||
return loose||isErrorInstance(value)?value:normalizeException(value); | ||
}; | ||
//# sourceMappingURL=main.js.map |
@@ -23,3 +23,3 @@ import isPlainObj from"is-plain-obj"; | ||
const validateAllOptions=function({ | ||
normalize, | ||
loose, | ||
shallow, | ||
@@ -32,3 +32,3 @@ beforeSerialize, | ||
{ | ||
validateBoolean(normalize,"normalize"); | ||
validateBoolean(loose,"loose"); | ||
validateBoolean(shallow,"shallow"); | ||
@@ -87,3 +87,3 @@ validateOptionalFunction(beforeSerialize,"beforeSerialize"); | ||
const addDefaultOptions=function({ | ||
normalize=false, | ||
loose=false, | ||
shallow=false, | ||
@@ -97,3 +97,3 @@ classes={}, | ||
return{ | ||
normalize, | ||
loose, | ||
shallow, | ||
@@ -100,0 +100,0 @@ classes, |
@@ -39,6 +39,6 @@ /** | ||
* ```js | ||
* console.log(serialize([{ error: new Error('test') }])) | ||
* // [{ error: { name: 'Error', ... } }] | ||
* console.log(serialize([{ error: new Error('test') }], { shallow: true })) | ||
* // [{ error: Error }] | ||
* const error = new Error('test') | ||
* error.prop = new Error('prop') | ||
* serialize(error).prop // { name: 'Error', message: 'prop', ... } | ||
* serialize(error, { shallow: true }).prop // Error: prop ... | ||
* ``` | ||
@@ -49,3 +49,4 @@ */ | ||
/** | ||
* Convert `errorInstance` to an `Error` instance if it is not one. | ||
* By default, when the argument is not an `Error` instance, it is converted | ||
* to one. If this option is `true`, it is kept as is instead. | ||
* | ||
@@ -56,7 +57,7 @@ * @default false | ||
* ```js | ||
* console.log(serialize('example')) // 'example' | ||
* console.log(serialize('example', { normalize: true })) // { name: 'Error', message: 'example', ... } | ||
* serialize('example') // { name: 'Error', message: 'example', ... } | ||
* serialize('example', { loose: true }) // 'example' | ||
* ``` | ||
*/ | ||
readonly normalize?: boolean | ||
readonly loose?: boolean | ||
@@ -107,2 +108,3 @@ /** | ||
* const errorObjects = serialize(errors, { | ||
* loose: true, | ||
* // Serialize `Date` instances as strings | ||
@@ -120,2 +122,3 @@ * beforeSerialize(error) { | ||
* const newErrors = parse(errorObjects, { | ||
* loose: true, | ||
* // Parse date strings as `Date` instances | ||
@@ -165,7 +168,3 @@ * beforeParse(errorObject) { | ||
Options extends SerializeOptions, | ||
> = Options['normalize'] extends true | ||
? Value extends Error | ||
? Value | ||
: Error | ||
: Value | ||
> = Options['loose'] extends true ? Value : Value extends Error ? Value : Error | ||
@@ -205,8 +204,8 @@ type SerializeShallow<Value> = Value extends Error | ||
* ```js | ||
* const errorObject = serialize(new Error('test')) | ||
* const error = new Error('test') | ||
* error.prop = new Error('prop') | ||
* const errorObject = serialize(error) | ||
* | ||
* console.log(parse([{ error: errorObject }])) | ||
* // [{ error: Error }] | ||
* console.log(parse([{ error: errorObject }], { shallow: true })) | ||
* // [{ error: { name: 'Error', ... } }] | ||
* parse(errorObject).prop // Error: prop ... | ||
* parse(errorObject, { shallow: true }).prop // { name: 'Error', message: ... } | ||
* ``` | ||
@@ -217,3 +216,4 @@ */ | ||
/** | ||
* Convert `errorObject` to an error plain object if it is not one. | ||
* By default, when the argument is not an error plain object, it is | ||
* converted to one. If this option is `true`, it is kept as is instead. | ||
* | ||
@@ -224,7 +224,7 @@ * @default false | ||
* ```js | ||
* console.log(parse('example')) // 'example' | ||
* console.log(parse('example', { normalize: true })) // Error: example | ||
* parse('example') // Error: example | ||
* parse('example', { loose: true }) // 'example' | ||
* ``` | ||
*/ | ||
readonly normalize?: boolean | ||
readonly loose?: boolean | ||
@@ -257,2 +257,3 @@ /** | ||
* const errorObjects = serialize(errors, { | ||
* loose: true, | ||
* // Serialize `Date` instances as strings | ||
@@ -270,2 +271,3 @@ * beforeSerialize(error) { | ||
* const newErrors = parse(errorObjects, { | ||
* loose: true, | ||
* // Parse date strings as `Date` instances | ||
@@ -294,2 +296,3 @@ * beforeParse(errorObject) { | ||
* const errorObjects = serialize(errors, { | ||
* loose: true, | ||
* // Serialize `Date` instances as strings | ||
@@ -307,2 +310,3 @@ * beforeSerialize(error) { | ||
* const newErrors = parse(errorObjects, { | ||
* loose: true, | ||
* // Parse date strings as `Date` instances | ||
@@ -352,7 +356,3 @@ * beforeParse(errorObject) { | ||
Options extends ParseOptions, | ||
> = Options['normalize'] extends true | ||
? Value extends Error | ||
? Value | ||
: Error | ||
: Value | ||
> = Options['loose'] extends true ? Value : Value extends Error ? Value : Error | ||
@@ -359,0 +359,0 @@ type ParseShallow< |
{ | ||
"name": "error-serializer", | ||
"version": "4.2.0", | ||
"version": "5.0.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -15,3 +15,4 @@ [](https://www.npmjs.com/package/error-serializer) | ||
- Can be used as [`error.toJSON()`](#errortojson) | ||
- [Deep serialization/parsing](#shallow), including [event callbacks](#events) | ||
- [Deep serialization/parsing](#deep-serializationparsing), including | ||
[event callbacks](#events) | ||
- [Custom serialization/parsing](#custom-serializationparsing) (e.g. YAML or | ||
@@ -26,3 +27,3 @@ `process.send()`) | ||
[`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) | ||
- [Normalizes](#normalize) invalid errors | ||
- [Normalizes](#loose) invalid errors | ||
- Safe: this never throws | ||
@@ -64,3 +65,3 @@ | ||
`options` [`Options?`](#options)\ | ||
_Return value_: `object` | ||
_Return value_: `ErrorObject` | ||
@@ -81,8 +82,12 @@ Convert an `Error` instance into a plain object. | ||
<!-- eslint-disable no-unused-expressions --> | ||
```js | ||
serialize([{ error: new Error('test') }]) // [{ error: { name: 'Error', ... } }] | ||
serialize([{ error: new Error('test') }], { shallow: true }) // [{ error: Error }] | ||
const error = new Error('test') | ||
error.prop = new Error('prop') | ||
serialize(error).prop // { name: 'Error', message: 'prop', ... } | ||
serialize(error, { shallow: true }).prop // Error: prop ... | ||
``` | ||
#### normalize | ||
#### loose | ||
@@ -92,7 +97,8 @@ _Type_: `boolean`\ | ||
Convert `errorInstance` to an `Error` instance if it is not one. | ||
By default, when the argument is not an `Error` instance, it is converted to | ||
one. If this option is `true`, it is kept as is instead. | ||
```js | ||
serialize('example') // 'example' | ||
serialize('example', { normalize: true }) // { name: 'Error', message: 'example', ... } | ||
serialize('example') // { name: 'Error', message: 'example', ... } | ||
serialize('example', { loose: true }) // 'example' | ||
``` | ||
@@ -148,12 +154,14 @@ | ||
<!-- eslint-disable no-unused-expressions --> | ||
```js | ||
const errorObject = serialize(new Error('test')) | ||
const error = new Error('test') | ||
error.prop = new Error('prop') | ||
const errorObject = serialize(error) | ||
parse([{ error: errorObject }]) | ||
// [{ error: Error }] | ||
parse([{ error: errorObject }], { shallow: true }) | ||
// [{ error: { name: 'Error', ... } }] | ||
parse(errorObject).prop // Error: prop ... | ||
parse(errorObject, { shallow: true }).prop // { name: 'Error', message: ... } | ||
``` | ||
#### normalize | ||
#### loose | ||
@@ -163,7 +171,8 @@ _Type_: `boolean`\ | ||
Convert `errorObject` to an error plain object if it is not one. | ||
By default, when the argument is not an error plain object, it is converted to | ||
one. If this option is `true`, it is kept as is instead. | ||
```js | ||
parse('example') // 'example' | ||
parse('example', { normalize: true }) // Error: example | ||
parse('example') // Error: example | ||
parse('example', { loose: true }) // 'example' | ||
``` | ||
@@ -253,2 +262,17 @@ | ||
## Deep serialization/parsing | ||
The [`loose` option](#loose) can be used to deeply serialize/parse objects and | ||
arrays. | ||
```js | ||
const error = new Error('example') | ||
const deepArray = serialize([{}, { error }], { loose: true }) | ||
const jsonString = JSON.stringify(deepArray) | ||
const newDeepArray = JSON.parse(jsonString) | ||
const newError = parse(newDeepArray, { loose: true })[1].error // Error: example | ||
``` | ||
## Events | ||
@@ -263,2 +287,3 @@ | ||
const errorObjects = serialize(errors, { | ||
loose: true, | ||
// Serialize `Date` instances as strings | ||
@@ -276,2 +301,3 @@ beforeSerialize(error) { | ||
const newErrors = parse(errorObjects, { | ||
loose: true, | ||
// Parse date strings as `Date` instances | ||
@@ -278,0 +304,0 @@ beforeParse(errorObject) { |
37027
426
715