-
Notifications
You must be signed in to change notification settings - Fork 142
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
Add file/line information to main error type with macros #653
Conversation
4673bfc
to
7ff5675
Compare
Overall, these changes are pretty good. It's a shame that the syntax can't be nicer, but I understand it'd require a lot more effort to get it to look more natural. One question:
I have one really hacky idea for that, but it'd be terribly hard to do, so I'll leave the topic be if there's no obvious solution. |
7ff5675
to
48daef9
Compare
Yeah, I definitely agree having some kind of compile time enforcement for calling |
613bf3a
to
e85f846
Compare
…th helpful macros
e85f846
to
ccb3845
Compare
This closes #574, by adding diagnostic information to the main error type along with some helpful macros.
Motivation
For internal server errors, we want to visibility into both the underlying error, in addition to where the error originated from. e.g. for code like this
Just looking at the logs, it can be hard to diagnose whether the underlying db error came from
Application.fetch(...)?
orapp.save()?
.We want to be able to quickly correlate an error to a specific line, while still keeping error handling relatively boilerplate free.
Solution
First, we extend the
error::Error
type with some diagnostic information - literally just the line number and file name.To avoid really boilerplate heavy code, most of the
error::Error
initialization logic is now handled by macros.queue_err!
,generic_err!
, etc. This makes it pretty easy to add the file/line information without making error initialization excessively verbose (in some cases, we're actually able to make error initialization easier than before).One point of friction is that we can't easily attach
location
information when propagating errors using?
- which we lean on pretty heavily when convertingDbErr
,RedisError
,bb8::RunError
, etc. Adding the location information to an error should be easy, but it should also be something that is difficult to forget to do.To circumvent this problem, I dropped the
From<...> for Error
implementations forDbErr
,RedisError
, etc. These were replaced with a very similar trait to just transformsstd::result::Result<T, E>
intocrate::error::Result<T>
forDbErr
,RedisError
, etc. There's then actx!
macro that handles the conversion, and adds thelocation
information. Now error handling looks closer tooSo, marginally more boilerplate. But forgetting to include
ctx!
now becomes a compiler error, forcing developers to add the diagnostic information usingctx!
.In addition,
ctx!
adds tracing information if the error is already acrate::result::Result
.What does this end up looking like in practice? If you have code like this
when the error is transformed into a response from the axum handlers, we get logs like