-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
Wrapping other Errors without losing diagnostics #172
Comments
/cc @matthiasbeyer looks like something might have gone sideways with |
#169 seems to fix the compilation error but I still don’t get any diagnostics from the nested error. Current code is: #[derive(Error, Diagnostic, Debug)]
pub enum Error {
// …
#[error("Input file had a syntax error")]
#[diagnostic(code(mylib::syntax_error))]
SyntaxErr(
#[from]
#[diagnostic_source]
knuffel::Error,
),
} Output is:
(Have tried the variant with a single-field struct as well, with the same result.) Edit: Oh, is this expected behaviour? Quoting the
|
This is a bit annoying as we are forced to use #[related] here which isn't quite what we want. Ideally, this would use #[diagnostic_source] but, there's a bug upstream. See: zkat/miette#172.
Heya, I'd like to revive this discussion. I've been keeping my project compatible with
Each of these 3 crates provides Example error types:// Framework crate
#[derive(miette::Diagnostic)]
enum FrameworkError {
One {
#[source_code] source: miette::NamedSource,
#[label] error_span: Option<miette::SourceOffset>,
},
}
// Plugin crate, depends on framework
#[derive(miette::Diagnostic)]
enum PluginError {
One {
#[source_code] source: miette::NamedSource,
#[label] error_span: Option<miette::SourceOffset>,
},
Two { .. },
FrameworkError(
#[diagnostic_source]
#[source]
FrameworkError,
),
}
// Consumer crate, depends on Framework and Plugin crates
#[derive(miette::Diagnostic)]
enum ConsumerError {
One {
#[source_code] source: miette::NamedSource,
#[label] error_span: Option<miette::SourceOffset>,
},
FrameworkError(
#[diagnostic_source]
#[source]
FrameworkError,
),
PluginError(
#[diagnostic_source]
#[source]
PluginError,
),
} Currently when an error is returned, the // error is a `ConsumerError`
let diagnostic: &dyn Diagnostic = match error {
// Repeat this for all the plugins
ConsumerError::PluginError(PluginError::FrameworkError(e)) => e,
ConsumerError::PluginError(e) => e,
ConsumerError::FrameworkError(e) => e,
_ => error,
}; So ideally the printing of those inner diagnostics could / would be automatic, since the Given the above, I propose the following behaviour:
Would that be the right direction for |
This is now solved thanks to #170. |
I’m having trouble figuring out how to directly (i.e. not with a set of errors like
related
) wrap another Miette-embellished error type without losing its diagnostic information.I started with (for example):
However, when printing this drops the diagnostic information from the
knuffel::Error
.It seems like
source
/diagnostic_source
are the solution to this (andfrom
impliessource
), however if I use them like this:… I get problems with the
Diagnostic
macro:#[diagnostic_source]
actually doesn’t fail if I lift it onto the enum variant (as opposed to its field) but then it doesn’t seem to have any effect.I feel like this should be something straightforward, what am I missing? 😊
The text was updated successfully, but these errors were encountered: