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

Nested Error's are not deserialized #46

Closed
jessetabak opened this issue May 12, 2021 · 2 comments · Fixed by #69
Closed

Nested Error's are not deserialized #46

jessetabak opened this issue May 12, 2021 · 2 comments · Fixed by #69

Comments

@jessetabak
Copy link

When I use nested Error's they are serialized, but not deserialized, am I missing something?:

const {serializeError, deserializeError} = require('serialize-error');

class MyError extends Error {    
    constructor(message, innerError) {
        super(message);

        this.innerError = innerError;
    }
}

const error = new MyError('Error message', new Error('inner error'));
const serialized = serializeError(error)
console.log(serialized);
/* outputs:
{
    name: 'Error',
    message: 'Error message',
    stack: '<cut>',
    innerError: {
        name: 'Error',
        message: 'inner error',
        stack: '<cut>'
    }
}
*/
const deserialized = deserializeError(serialized);
console.log(deserialized);
/* outputs:
Error: Error message
    at <cut> {
  innerError: {}
}
*/
@sindresorhus
Copy link
Owner

There's no way for the deserializeError method to know that the innerError object is actually an Error type. The unserialized value it came from could have been a plain object.

There could indeed be an option to have such a behavior, but it's not a safe default.

@fregante
Copy link
Contributor

fregante commented Jun 26, 2021

I think this could be turned into a generic "deep serialize error" feature, so that I could serialize and deserialize the output of Promise.allSettled for example.

Deep-serialize already seems to work: https://runkit.com/embed/eu5uful88qx9

const {serializeError} = require("serialize-error")

const all = await Promise.allSettled([
    Promise.resolve('1'),
    Promise.reject(new Error('Sorry John'))
]);

serializeError(all);

But deserializeError would return a NonError


Also either this or #48 could add support for AggregateError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants