From 6b2ec67b2c18928afadf20324b571cf6f480c301 Mon Sep 17 00:00:00 2001 From: Iain Shorter Date: Mon, 16 Sep 2024 17:04:04 +0000 Subject: [PATCH 1/2] add support for Error cause --- src/transformer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transformer.ts b/src/transformer.ts index 70ac517..b7316e7 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -90,6 +90,7 @@ const simpleRules = [ const baseError: any = { name: v.name, message: v.message, + cause: v.cause }; superJson.allowedErrorProps.forEach(prop => { @@ -99,7 +100,7 @@ const simpleRules = [ return baseError; }, (v, superJson) => { - const e = new Error(v.message); + const e = new Error(v.message, { cause: v.cause }); e.name = v.name; e.stack = v.stack; From 55ad71ceb29a3bfa9f51b2b717f7b93c1ca3807d Mon Sep 17 00:00:00 2001 From: Iain Shorter Date: Mon, 16 Sep 2024 17:30:49 +0000 Subject: [PATCH 2/2] change error to a "deep" type --- src/plainer.ts | 2 ++ src/transformer.ts | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) 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 b7316e7..fba31be 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -23,7 +23,6 @@ type LeafTypeAnnotation = | PrimitiveTypeAnnotation | 'regexp' | 'Date' - | 'Error' | 'URL'; type TypedArrayAnnotation = ['typed-array', string]; @@ -31,7 +30,7 @@ type ClassTypeAnnotation = ['class', string]; type SymbolTypeAnnotation = ['symbol', string]; type CustomTypeAnnotation = ['custom', string]; -type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set'; +type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set' | 'Error'; type CompositeTypeAnnotation = | TypedArrayAnnotation