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

Error subclasses are lost when deserializing #48

Closed
fregante opened this issue Jun 26, 2021 · 7 comments · Fixed by #70
Closed

Error subclasses are lost when deserializing #48

fregante opened this issue Jun 26, 2021 · 7 comments · Fixed by #70

Comments

@fregante
Copy link
Contributor

fregante commented Jun 26, 2021

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

class MyError extends Error {
    constructor(message) {
        super(message);
        this.name = 'MyError'
    }
}

const original = new MyError('This is my error');
const serialized = serializeError(original);
const deserialized = deserializeError(serialized);

original will be a MyError but deserialized will be a plain Error

Live example: https://runkit.com/embed/f46ewcgxyxqg

I think deserializeError should support custom error types, if passed as an argument (so they are known and explicit):

const deserialized = deserializeError(serialized, {constructors: [MyError, BusinessError, ErrorError]});

Native errors also should be automatically supported, like TypeError

@fregante
Copy link
Contributor Author

The good part is that name is preserved as 'MyError' even though it's not an instance of it.

@sindresorhus
Copy link
Owner

Good idea 👍

@fregante
Copy link
Contributor Author

fregante commented Dec 8, 2021

I think this could have an internal list of known constructors that could be extended/overridden by the user. Some can be found at: https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/error

Actually supporting all those error correctly might not be possible, but a plain if error.name === TypeError.name, then new TypeError(error.message) would go a long way.

@fregante
Copy link
Contributor Author

What do you think about exporting a global set of supported errors? This would avoid having to manually pass this around (and potentially forget to), as well enabling support across modules (and sub dependencies)

import {errorClasses} from 'serialize-error';

errorClasses.push(MyError, CoolError, APIError);

👆 Maybe an array is better because Set does not allow adding multiple items (or an array) at once.

@sindresorhus
Copy link
Owner

Sure. I don't think it should contain the built-in error types though. We don't want users to be able to remove those. So maybe it should be named customErrorClasses? I would go with Set. It's the correct data structure for this.

rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 23, 2022
…P response

- Also sets the status code dynamically based on presence of tx submission errors.
- Note: This currently looses the custom type precision, however it appears there's
a feature in development to address this:
sindresorhus/serialize-error#48
- Tests are also improved to avoid false positives
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 23, 2022
…ubmitHttpProvider

- Note: While this rehydrates errors, until
sindresorhus/serialize-error#48, the type precision is lost.
The error.name can be used to differentiate the type for now.
- Also improves tests to avoid false-positives
- Fixes JSDoc references
mkazlauskas pushed a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 23, 2022
…P response

- Also sets the status code dynamically based on presence of tx submission errors.
- Note: This currently looses the custom type precision, however it appears there's
a feature in development to address this:
sindresorhus/serialize-error#48
- Tests are also improved to avoid false positives
mkazlauskas pushed a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 23, 2022
…ubmitHttpProvider

- Note: While this rehydrates errors, until
sindresorhus/serialize-error#48, the type precision is lost.
The error.name can be used to differentiate the type for now.
- Also improves tests to avoid false-positives
- Fixes JSDoc references
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 24, 2022
- uses the `TxSubmitProvider` interface to facilitate both queue and direct submission
- /submit accepts cbor-encoded binary data to remain aligned with other Cardano APIs
- serialize tx submit errors in HTTP response
- Sets the status code dynamically based on presence of tx submission errors.
- Note: This currently looses the custom type precision, however it appears there's
a feature in development to address this:
sindresorhus/serialize-error#48
- CLI entrypoint using Commander
- run entrypoint for ENV configuration

feat(cardano-graphql-services): configurable request limit in TxSubmitHttpServer
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 24, 2022
- Uses got as the HTTP request library. It's pinned at the previous
major version as the latest is incompatible with CommonJS, so I've elected
stay on v11 until we look at our module strategy and targets.
sindresorhus/got#1789
- deserialize and throw domain errors
- Note: While this rehydrates errors, until
sindresorhus/serialize-error#48, the type precision is lost.
The error.name can be used to differentiate the type for now.
- Fixes JSDoc references
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 24, 2022
- uses the `TxSubmitProvider` interface to facilitate both queue and direct submission
- /submit accepts cbor-encoded binary data to remain aligned with other Cardano APIs
- serialize tx submit errors in HTTP response
- Sets the status code dynamically based on presence of tx submission errors.
- Note: This currently looses the custom type precision, however it appears there's
a feature in development to address this:
sindresorhus/serialize-error#48
- CLI entrypoint using Commander
- run entrypoint for ENV configuration

feat(cardano-graphql-services): configurable request limit in TxSubmitHttpServer
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 24, 2022
- Uses got as the HTTP request library. It's pinned at the previous
major version as the latest is incompatible with CommonJS, so I've elected
stay on v11 until we look at our module strategy and targets.
sindresorhus/got#1789
- deserialize and throw domain errors
- Note: While this rehydrates errors, until
sindresorhus/serialize-error#48, the type precision is lost.
The error.name can be used to differentiate the type for now.
- Fixes JSDoc references
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 25, 2022
- uses the `TxSubmitProvider` interface to facilitate both queue and direct submission
- /submit accepts cbor-encoded binary data to remain aligned with other Cardano APIs
- serialize tx submit errors in HTTP response
- Sets the status code dynamically based on presence of tx submission errors.
- Note: This currently looses the custom type precision, however it appears there's
a feature in development to address this:
sindresorhus/serialize-error#48
- CLI entrypoint using Commander
- run entrypoint for ENV configuration
- configurable request limit in TxSubmitHttpServer
- ensure txSubmitProvider is healthy on TxSubmitHttpServer init, otherwise throw
`ProviderFailure.Unhealthy`.
- throw provider error if unhealthy during TxSubmitHttpServer submit, and return 503
 HTTP status code
rhyslbw added a commit to input-output-hk/cardano-js-sdk that referenced this issue Mar 25, 2022
- Uses got as the HTTP request library. It's pinned at the previous
major version as the latest is incompatible with CommonJS, so I've elected
stay on v11 until we look at our module strategy and targets.
sindresorhus/got#1789
- deserialize and throw domain errors
- Note: While this rehydrates errors, until
sindresorhus/serialize-error#48, the type precision is lost.
The error.name can be used to differentiate the type for now.
- Fixes JSDoc references
@StevenLangbroek
Copy link

Hey @sindresorhus! Would you be open to another PR adding support for AggregateError?

@fregante
Copy link
Contributor Author

fregante commented Oct 9, 2022

You can open a new issue. Given it’s a complex error, it first needs proper serialization support.

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