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

Rework error handling example #2382

Merged
merged 2 commits into from
Dec 29, 2023
Merged

Conversation

davidpdrsn
Copy link
Member

This reworks the ye olde error-handling-and-dependency-injection. I've removed all the dependency injection bits because I think they're distracting and deserve their own example.

Hopefully this new version is easier to follow and can serve as a template for people to build upon.

@davidpdrsn davidpdrsn added the T-docs Topic: documentation label Nov 29, 2023
@davidpdrsn davidpdrsn requested a review from jplatte November 29, 2023 21:58
.get::<MatchedPath>()
.map(|matched_path| matched_path.as_str());

tracing::debug_span!("request", %method, %uri, matched_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As-is, the uri (and method) won't be in quotes but the matched path will, as the comment at the start of the file shows correctly. I find this pretty weird. Maybe use let uri = req.uri().to_string(); above instead, and remove the % before uri here? For the method, it probably makes sense for it not to be in quotes.

AppError::TimeError(err) => {
// Because `TraceLayer` wraps each request in a span that contains the request
// method, uri, etc we don't need to include those details here
tracing::error!(%err, "error from time_library");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe controversial, but I find the err= bit in the output a bit weird. IMHO tracing fields are much more useful for fields whose exact value you'd want to match when searching logs, rather than error messages that are generally just searched for specific keywords.

Suggested change
tracing::error!(%err, "error from time_library");
tracing::error!("error from time_library: {err}");

@davidpdrsn davidpdrsn merged commit c3db223 into main Dec 29, 2023
18 checks passed
@davidpdrsn davidpdrsn deleted the david/rework-error-handling-example branch December 29, 2023 00:49
@NVedsted
Copy link

Not sure what the chances are of me stumbling upon links to the deleted example just as this merges, haha. I just wanted to highlight these references to the deleted/renamed file:

  • * [`error-handling-and-dependency-injection`][ehdi] for application-specific detailed errors
    [anyhow]: https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs
    [ehdi]: https://github.com/tokio-rs/axum/blob/main/examples/error-handling-and-dependency-injection/src/main.rs
  • //! * [`error-handling-and-dependency-injection`][ehdi] for application-specific detailed errors
    //!
    //! [anyhow]: https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs
    //! [ehdi]: https://github.com/tokio-rs/axum/blob/main/examples/error-handling-and-dependency-injection/src/main.rs

Not familiar with contributing, so let me know if an issue would be preferable. :)

@davidpdrsn
Copy link
Member Author

Thanks for noticing! I've filed an issue #2464

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants