You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to propose extending the usage of #[error(transparent)] (or maybe adding a new #[transparent] attribute) for handling cases when there are other fields in the enum variant (similar to #103).
Motivation
I'm using thiserror for defining enum-like errors and each of them can have many variants, but my case is similar to:
But this is also not working cause of error[E0658]: use of unstable library feature 'error_generic_member_access'.
I discovered #314 explaining that this is expected that the usage of Backtrace requires nightly. But I got curious why it generated unstable nightly code on stable in the first place. After some digging, I found #223 where is explained that this is done to be future-compatible if I understand correctly.
So, I tried to remove the Backtrace type by using a type alias:
The main drawback is that I need to specify some error message in #[error()]. This can be not a problem (e.g. IO error), but in my case of opaque error, it should indeed be strictly transparent.
Workaround 2
Another workaround would be to leave only one field with #[error(transparent)] but to change its type to include a backtrace inside it:
This indeed works as expected by now I need to maintain an additional Opaque type.
Workaround 3
I could use anyhow::Error instead of Box<dyn std::error::Error> and backtrace, but in this case, I cannot use anyhow::Error as for certain reasons I need standard Debug impl (to print backtraces from nested errors, as I bumped also into #174).
Workaround 4
I can switch from thiserror to manual traits implementation, but some of the enum-like errors are huge and it would require a considerable amount of time to change.
I think this also can be usable with Location (#142) or any other field as well, so something like this would be possible:
I've merged this issue into #103. There is no relevant distinction between structs vs enums for this purpose. The feature request is for #[error(transparent)] on fields.
Summary
I would like to propose extending the usage of
#[error(transparent)]
(or maybe adding a new#[transparent]
attribute) for handling cases when there are other fields in the enum variant (similar to #103).Motivation
I'm using
thiserror
for defining enum-like errors and each of them can have many variants, but my case is similar to:Sometime later now I want to modify this one variant to support a backtrace, so I tried this:
But this is not working because of
error: #[error(transparent)] requires exactly one field
.Workaround 1
I can work around it by using a custom message in the
Display
impl:But this is also not working cause of
error[E0658]: use of unstable library feature 'error_generic_member_access'
.I discovered #314 explaining that this is expected that the usage of
Backtrace
requires nightly. But I got curious why it generated unstable nightly code on stable in the first place. After some digging, I found #223 where is explained that this is done to be future-compatible if I understand correctly.So, I tried to remove the
Backtrace
type by using a type alias:This indeed helped to not generate nightly code, but now it fails with
error: deriving From requires no fields other than source and backtrace
.This is not a problem cause I still planned to capture the backtrace in the
From
impl, so finally now I have a fully working version:Drawbacks
The main drawback is that I need to specify some error message in
#[error()]
. This can be not a problem (e.g.IO error
), but in my case of opaque error, it should indeed be strictly transparent.Workaround 2
Another workaround would be to leave only one field with
#[error(transparent)]
but to change its type to include a backtrace inside it:Drawbacks
This indeed works as expected by now I need to maintain an additional
Opaque
type.Workaround 3
I could use
anyhow::Error
instead ofBox<dyn std::error::Error>
and backtrace, but in this case, I cannot useanyhow::Error
as for certain reasons I need standardDebug
impl (to print backtraces from nested errors, as I bumped also into #174).Workaround 4
I can switch from
thiserror
to manual traits implementation, but some of the enum-like errors are huge and it would require a considerable amount of time to change.I think this also can be usable with
Location
(#142) or any other field as well, so something like this would be possible:Thanks for such a great crate.
The text was updated successfully, but these errors were encountered: