Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AggregateError supprt #297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/plainer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
isArray,
isEmptyObject,
isError,
isMap,
isPlainObject,
isPrimitive,
Expand Down Expand Up @@ -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<any, any[][]>) {
Expand Down
34 changes: 32 additions & 2 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isTypedArray,
TypedArrayConstructor,
isURL,
isAggregateError,
} from './is.js';
import { findArr } from './util.js';
import SuperJSON from './index.js';
Expand All @@ -23,15 +24,14 @@ type LeafTypeAnnotation =
| PrimitiveTypeAnnotation
| 'regexp'
| 'Date'
| 'Error'
| 'URL';

type TypedArrayAnnotation = ['typed-array', string];
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
Expand Down Expand Up @@ -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',
Expand Down