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

Repetition in nested error messages #21

Open
andrewhickman opened this issue Jun 27, 2018 · 3 comments
Open

Repetition in nested error messages #21

andrewhickman opened this issue Jun 27, 2018 · 3 comments

Comments

@andrewhickman
Copy link

andrewhickman commented Jun 27, 2018

I'm trying to implement a kind of user-extensible deserialization that allows you to "plug in" extra deserialize implementations. The basic idea is to have a Seed struct containing a HashMap<String, fn (&Seed, &mut erased_serde::Deserializer) -> erased_serde::Error> and use serde::DeserializeSeed to deserialize stuff. This is working pretty well, but there's an annoying issue with the error handling.

Whenever I call one of the deserialize implementations I can get an erased_serde::Error, but to bubble it up I need to convert it to an error for the deserializer I am using (serde_json at the moment, but I would like to remain generic), by using serde::de::Error::custom. These calls can be fairly recursive, so I might end up going back-and-forth between the two error types a few times. This is giving me error messages that look like this:

Error("trailing comma at line 32 column 41 at line 32 column 41 at line 33 column 37 at line 34 column 33 at line 35 column 29 at line 36 column 25 at line 36 column 25 at line 36 column 25 at line 36 column 25", line: 36, column: 25)

The ideal solution for me would be the ability to cast erased_serde::Error back to the original error type, by representing it as a Box<serde::de::Error>. I'm not sure whether my use-case is typical for this library so possibly this makes no sense in the general case, but I am interested to know what you think.

@dtolnay
Copy link
Owner

dtolnay commented Jun 27, 2018

Thanks for reporting this! I would absolutely be interested in supporting your use case better. Box<Error> seems like a reasonable approach to try. Would you like to try it out and send a PR?

@andrewhickman
Copy link
Author

I've had a go into implementing this and unfortunately I'm not sure its feasible. The issue is that there is no 'static bound on serializer/deserializer errors meaning any kind of downcasting approach is not possible.

@dtolnay
Copy link
Owner

dtolnay commented Jan 23, 2019

I implemented a JSON-specific workaround in serde_json 1.0.37 so this problem should no longer occur when using JSON, but I'll keep this issue open as we find a way to fix it in erased-serde generally for all formats.

@dtolnay dtolnay changed the title Problem with error handling Repetition in nested error messages Mar 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants