serde_urlencoded icon indicating copy to clipboard operation
serde_urlencoded copied to clipboard

Improve error messages; in particular, include key when value fails.

Open adeschamps opened this issue 3 years ago • 0 comments

This (partially) addresses #87.

This replaces the use of serde::de::value::MapDeserializer with a custom deserializer implementation. MapDeserializer is generic over the types of keys -- even if they're not displayable. However, for formats that use strings as keys, we can keep track of the key while parsing its value, and give more informative error messages.

The added tests show the difference between the error messages before and the error messages with the changes to src/de.rs:

---- deserialize_error_no_value stdout ----
thread 'deserialize_error_no_value' panicked at 'assertion failed: `(left == right)`
  left: `"cannot parse integer from empty string"`,
 right: `"failed to parse value for key 'first': cannot parse integer from empty string"`', tests/test_deserialize.rs:95:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- deserialize_error_struct stdout ----
thread 'deserialize_error_struct' panicked at 'assertion failed: `(left == right)`
  left: `"invalid digit found in string"`,
 right: `"failed to parse value for key 'second': invalid digit found in string"`', tests/test_deserialize.rs:121:5

---- deserialize_error_vec stdout ----
thread 'deserialize_error_vec' panicked at 'assertion failed: `(left == right)`
  left: `"invalid digit found in string"`,
 right: `"failed to parse value for key 'second': invalid digit found in string"`', tests/test_deserialize.rs:104:5

adeschamps avatar Aug 20 '22 12:08 adeschamps