reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Keep body around in `Error` when JSON decoding fails

Open mneumann opened this issue 3 years ago • 2 comments

  • Sometimes you want to inspect the JSON data that failed to parse.

mneumann avatar May 26 '22 15:05 mneumann

If desired, it is possible to facilitate this by using .bytes() or .text() instead of .json(), and then doing serde_json::from_slice/from_str as appropriate. If that fails, the Bytes/String structure will be available to you.

neoeinstein avatar Jun 13 '22 01:06 neoeinstein

If desired, it is possible to facilitate this by using .bytes() or .text() instead of .json(), and then doing serde_json::from_slice/from_str as appropriate. If that fails, the Bytes/String structure will be available to you.

yes, that's what I am basically doing. Actually, I wrote a RequestExt trait that adds a json_with_error_logging method that will error! out the failed JSON.

If the failed JSON body would accessible in the error Result, I could instead decide what to do with the error (whether to log it or not), later on in the error handler.

mneumann avatar Jun 21 '22 13:06 mneumann