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

Deserialize nested errors #69

Merged
merged 7 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ console.log(error);
```
*/
export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error;

/**
Predicate to determine whether an object looks like an error, even if it's not an instance of Error
*/
export function isErrorLike(error: unknown): error is ErrorObject;
36 changes: 24 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const destroyCircular = ({
}) => {
const to = to_ || (Array.isArray(from) ? [] : {});

console.log({to});
seen.push(from);

if (depth >= maxDepth) {
Expand All @@ -66,6 +67,16 @@ const destroyCircular = ({
return toJSON(from);
}

const destroyLocal = value => destroyCircular({
from: value,
seen: [...seen],
// eslint-disable-next-line unicorn/error-message
to_: isErrorLike(value) ? new Error() : undefined,
forceEnumerable,
maxDepth,
depth,
});

for (const [key, value] of Object.entries(from)) {
// eslint-disable-next-line node/prefer-global/buffer
if (typeof Buffer === 'function' && Buffer.isBuffer(value)) {
Expand All @@ -90,14 +101,8 @@ const destroyCircular = ({

if (!seen.includes(from[key])) {
depth++;
to[key] = destroyLocal(from[key]);

to[key] = destroyCircular({
from: from[key],
seen: [...seen],
forceEnumerable,
maxDepth,
depth,
});
continue;
}

Expand All @@ -107,7 +112,7 @@ const destroyCircular = ({
for (const {property, enumerable} of commonProperties) {
if (typeof from[property] !== 'undefined' && from[property] !== null) {
Object.defineProperty(to, property, {
value: from[property],
value: isErrorLike(from[property]) ? destroyLocal(from[property]) : from[property],
enumerable: forceEnumerable ? true : enumerable,
configurable: true,
writable: true,
Expand Down Expand Up @@ -148,16 +153,23 @@ export function deserializeError(value, options = {}) {
}

if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
const newError = new Error(); // eslint-disable-line unicorn/error-message
destroyCircular({
return destroyCircular({
from: value,
seen: [],
to_: newError,
// eslint-disable-next-line unicorn/error-message
to_: new Error(),
maxDepth,
depth: 0,
});
return newError;
}

return new NonError(value);
}

export function isErrorLike(error) {
return error
&& typeof error === 'object'
&& 'name' in error
&& 'message' in error
&& 'stack' in error;
}
36 changes: 35 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Buffer} from 'node:buffer';
import Stream from 'node:stream';
import test from 'ava';
import {serializeError, deserializeError} from './index.js';
import {serializeError, deserializeError, isErrorLike} from './index.js';

function deserializeNonError(t, value) {
const deserialized = deserializeError(value);
Expand Down Expand Up @@ -200,6 +200,28 @@ test('should deserialize plain object', t => {
t.is(deserialized.code, 'code');
});

test('should deserialize nested errors', t => {
fregante marked this conversation as resolved.
Show resolved Hide resolved
const object = {
message: 'error message',
stack: 'at <anonymous>:1:13',
name: 'name',
code: 'code',
innerError: {
message: 'source error message',
stack: 'at <anonymous>:3:14',
name: 'name',
code: 'code',
},
};

const {innerError} = deserializeError(object);
t.true(innerError instanceof Error);
t.is(innerError.message, 'source error message');
t.is(innerError.stack, 'at <anonymous>:3:14');
t.is(innerError.name, 'name');
t.is(innerError.code, 'code');
});

test('should deserialize the cause property', t => {
const object = {
message: 'error message',
Expand All @@ -215,6 +237,7 @@ test('should deserialize the cause property', t => {
};

const {cause} = deserializeError(object);
t.true(cause instanceof Error);
t.is(cause.message, 'source error message');
t.is(cause.stack, 'at <anonymous>:3:14');
t.is(cause.name, 'name');
Expand Down Expand Up @@ -383,3 +406,14 @@ test('should serialize properties up to `Options.maxDepth` levels deep', t => {
const levelThree = serializeError(error, {maxDepth: 3});
t.deepEqual(levelThree, {message, name, stack, one: {two: {three: {}}}});
});

test('should identify serialized errors', t => {
t.true(isErrorLike(serializeError(new Error('I\'m missing more than just your body'))));
// eslint-disable-next-line unicorn/error-message -- Testing this eventuality
t.true(isErrorLike(serializeError(new Error())));
t.true(isErrorLike({
name: 'Error',
message: 'Is it too late now to say sorry',
stack: 'at <anonymous>:3:14',
}));
});