You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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?
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.
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
changed the title
Problem with error handling
Repetition in nested error messages
Mar 28, 2020
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 aHashMap<String, fn (&Seed, &mut erased_serde::Deserializer) -> erased_serde::Error>
and useserde::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 usingserde::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 aBox<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.The text was updated successfully, but these errors were encountered: