Closed Douile closed 1 year ago
Looks really good, although, would we want to have backtracing as default in the library? Maybe have this as a feature?
I'm saying this just for regarding performance, but as this would happen once (error has been thrown and thats all), i don't think this would be a problem, and as you said that it may not be an issue because the backtrace is captured only if RUST_BACKTRACE
is set.
A macro could look like this:
macro_rules! error_kind_impl {
($kind: expr, $name: ident, $from_name: ident) => {
pub fn $name(source: Option<ErrorSource>) -> GDRichError {
GDRichError::new($kind, source)
}
pub fn $from_name<E: Into<Box<dyn std::error::Error + 'static>>>(source: E) -> GDRichError {
GDRichError::$name(Some(source.into()))
}
}
}
impl GDRichError {
// ...
error_kind_impl!(GDError::PacketUnderflow, packet_underflow, packet_underflow_from_into);
// ...
}
Unfortunately without proc macros both functions names would need to be specified (no way to use macros in place of idents: https://github.com/rust-lang/rust/issues/29599) and each variant needs to be listed.
I cannot believe how much I was over-complicating things. Implementing a single method on the error kind enum is way simpler and works better...
Things I would have to do for this PR to be usable (assuming everyone is happy):
GDError
to GDErrorKind
GDRichError
to GDError
GDError::kind
to ???GDError::BadGame
(this could be included in the source of the rich error)GDRichError
production readyGDErrorKind
Hey, regarding this point:
Possibly add a separate message field to source field, in case we want to include a message and a source
I think this should be discussed in #65, as this is about giving extra information, and this might be included in the enum by default, we can omit this in this PR.
Also, please rebase!
A first draft of implementing a better error type that propagates source errors and messages.
Requires bumping MSRV to 1.65.0 as it uses backtraces.
I didn't rename
GDError
as that would require a lot more changes throughout the codebase, however I changedGDResult
to use the newGDRichError
(which usesGDError
as its kind part) and added a From implementation soGDError
s that are not updated are implicitly converted to the newGDRichError
(with an added backtrace).Other than that its a pretty basic type but has a custom debug formatter so an unwrapped error output looks like this:
I need to look into whether implicitly capturing backtraces whenever an error is created (errors are created whenever they are used in or_err etc.) affects performance, although that may be a non-issue as backtraces are only captured if the
RUST_BACKTRACE
environment variable is set.Related #65
This error type only allows either an error message or an error source as the error message would be stored as a source by converting
&str
toBox<dyn std::error::Error + 'static>
.This doesn't address #65 point 5.