Skip to content

Conversation

@tomassedovic
Copy link
Contributor

We were converting the reqwest::Error type directly to span::Error and the rendering chain stopped at that. This ignored the inner (source) error which contained the most useful information to figure out what went wrong.

This gets us from:

Error: error: failed to fetch: error: error decoding response body

to:

Error: error: failed to fetch: error: error decoding response body: missing field `discord` at line 16049 column 1

We were converting the `reqwest::Error` type directly to `span::Error`
and the rendering chain stopped at that. This ignored the
inner (source) error which contained the most useful information to
figure out what went wrong.

This gets us from:

    Error: error: failed to fetch: error: error decoding response body

to:

    Error: error: failed to fetch: error: error decoding response body: missing field `discord` at line 16049 column 1
@tomassedovic
Copy link
Contributor Author

You can reproduce this on the following commit: 965abda (the underlying issue has been fixed in #423)

$ git checkout 965abda
$ cargo rpg updates 2025h2 --output-file out.html

It will return this error:

Error: error: failed to fetch: error: error decoding response body
   --> /home/shadower/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/spanned-0.6.1/src/error.rs:130:23
    |
130 |                 span: Span::here(),
    |                       ^^^^^^^^^^^^^
    |
  --> crates/rust-project-goals/src/team.rs:17:27
   |
17 |             Err(e) => Err(Error::str(format!("failed to fetch: {e:?}"))),
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |

That message tells you nothing about what's wrong.

With this PR, we get the following instead:

Error: error: failed to fetch: error: error decoding response body: missing field `discord` at line 16049 column 1
   --> crates/rust-project-goals/src/team.rs:133:13
    |
133 |             spanned::Error::str(format!("{e}{source_message}"))
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
  --> crates/rust-project-goals/src/team.rs:17:27
   |
17 |             Err(e) => Err(Error::str(format!("failed to fetch: {e:?}"))),
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |

You can see that the first line tells you a bit more about the issue (there's a missing field in the JSON it's trying to convert) and give you slightly better stacktrace (pointing you at the fetch function).

@tomassedovic
Copy link
Contributor Author

I don't love the spans here, they seem to add more noise than clarity. I also feel constructing a spaned::Error from a formatted string is kind of ugly.

Maybe there's a way to make sure spanned does the right thing with inner errors somehow instead?

The code is shorter and less hacky. The end-user message looks much
better too.
@tomassedovic
Copy link
Contributor Author

Ok, I think this is much better both in terms of the code and the message displayed to the user:

$ cargo rpg updates 2025h2 --output-file out
Error: error: failed to fetch: error: missing field `discord` at line 16049 column 1
   --> crates/rust-project-goals/src/team.rs:129:35
    |
129 |                 .map(|json_error| spanned::Error::str(format!("{json_error}")))
    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
  --> crates/rust-project-goals/src/team.rs:17:27
   |
17 |             Err(e) => Err(Error::str(format!("failed to fetch: {e:?}"))),
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |

@tomassedovic
Copy link
Contributor Author

Okay, I'm much happier with this version. Maybe there's a better way to handle this, but we just create a spanned::Error from the source error and the output looks good.

Happy to merge this unless there are objections.

@lqd
Copy link
Member

lqd commented Oct 30, 2025

Nice, the output error looks much better.

@lqd lqd merged commit 3aed4a6 into main Oct 30, 2025
2 checks passed
@lqd lqd deleted the json-decoder-error branch October 30, 2025 15:00
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

Successfully merging this pull request may close these issues.

3 participants