Closed MichaelScofield closed 7 months ago
At first glance, this doesn't feel like a good fit. Some points:
#[snafu(extend_display)]
.#[snafu(display("Foo, blabla: {err_msg}, at {location}")]
#[snafu(display("Foo, blabla: {err_msg}")]#[snafu(extend_display)]
, at
part of the string come from when using the proposed syntax?#[snafu(extend_display)]
be handled?One partial alternative that you could do today would be to create your own Location
type that wraps snafu::Location
and implement Display
to output the , at
part in addition to the inner location information. Then your attribute would be just #[snafu(display("Foo, blabla: {err_msg}{location}")]
Happy for you to correct any misunderstandings I have!
@shepmaster thx, that make sense. Then is it possible to display the whole error that automatically include all of its fields? For example:
#[derive(Debug, Snafu)]
enum MyError {
#[snafu(display)] // <-- If this "display" has no "()", then snafu will automatically make the "Foo { .. }" "to_string"ed.
Foo {
err_msg: String,
location: Location,
}
}
If the Foo
is annotated with #[snafu(display)]
, but without a display format, then snafu could automatically make the Foo
implemented std::fmt::Display
and the display of Foo
is something with predefined format like "Foo, err_msg: {err_msg}, location: {location}".
Found a similar question in stackoverflow: https://stackoverflow.com/questions/76903644/is-it-possible-to-automatically-add-location-to-snafu-display
Basically, I'd like to have something to let me include the "location" (or other commonly used information like "user_id" or "request_id") in the error's display, without explicitly write it everytime.
For example, without this feature, I have to include "location" manually in
#[snafu(display)]
:With it, snafu automatically include it for me:
If this feature request sounds good to you, I'd like be working on this issue. Thx!