diff --git a/src/is.ts b/src/is.ts index 438ea49..10782dc 100644 --- a/src/is.ts +++ b/src/is.ts @@ -49,6 +49,9 @@ export const isDate = (payload: any): payload is Date => export const isError = (payload: any): payload is Error => payload instanceof Error; +export const isAggregateError = (payload: any): payload is AggregateError => + payload instanceof AggregateError + export const isNaNValue = (payload: any): payload is typeof NaN => typeof payload === 'number' && isNaN(payload); diff --git a/src/plainer.ts b/src/plainer.ts index 114d76a..a78ba40 100644 --- a/src/plainer.ts +++ b/src/plainer.ts @@ -1,6 +1,7 @@ import { isArray, isEmptyObject, + isError, isMap, isPlainObject, isPrimitive, @@ -95,6 +96,7 @@ const isDeep = (object: any, superJson: SuperJSON): boolean => isArray(object) || isMap(object) || isSet(object) || + isError(object) || isInstanceOfRegisteredClass(object, superJson); function addIdentity(object: any, path: any[], identities: Map) { diff --git a/src/transformer.ts b/src/transformer.ts index 70ac517..7a115f1 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -13,6 +13,7 @@ import { isTypedArray, TypedArrayConstructor, isURL, + isAggregateError, } from './is.js'; import { findArr } from './util.js'; import SuperJSON from './index.js'; @@ -23,7 +24,6 @@ type LeafTypeAnnotation = | PrimitiveTypeAnnotation | 'regexp' | 'Date' - | 'Error' | 'URL'; type TypedArrayAnnotation = ['typed-array', string]; @@ -31,7 +31,7 @@ type ClassTypeAnnotation = ['class', string]; type SymbolTypeAnnotation = ['symbol', string]; type CustomTypeAnnotation = ['custom', string]; -type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set'; +type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set' | 'Error' | 'AggregateError'; type CompositeTypeAnnotation = | TypedArrayAnnotation @@ -83,6 +83,36 @@ const simpleRules = [ v => new Date(v) ), + simpleTransformation( + isAggregateError, + 'AggregateError', + (v, superJson) => { + const baseError: any = { + name: v.name, + message: v.message, + errors: v.errors, + cause: v.cause + }; + + superJson.allowedErrorProps.forEach(prop => { + baseError[prop] = (v as any)[prop]; + }); + + return baseError; + }, + (v, superJson) => { + const e = new AggregateError(v.errors, v.message, { cause: v.cause }); + e.name = v.name; + e.stack = v.stack; + + superJson.allowedErrorProps.forEach(prop => { + (e as any)[prop] = v[prop]; + }); + + return e; + } + ), + simpleTransformation( isError, 'Error',