Pure Source Error with Location #433
-
Hello! I'm looking for an ergonomic solution to adding error Here is my current code: #[derive(snafu::Snafu)]
enum Error {
...
#[snafu(display(
"{} ({}:{}:{})",
source,
location.file,
location.line,
location.column,
))]
Env {
source: std::env::VarError,
location: snafu::Location,
},
...
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
fn main() -> Result<(), Error> {
let variable = std::env::var("MISSING_VARIABLE").context(EnvSnafu)?;
} And the error response: Error: environment variable not found (src/main.rs:100:100) And a possible desired version: #[derive(snafu::Snafu)]
enum Error {
...
#[snafu(something_custom)]
Env,
...
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
fn main() -> Result<(), Error> {
let variable = std::env::var("MISSING_VARIABLE").context(EnvSnafu)?;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I can't immediately think of a great way to do this with SNAFU. I'm not sure I completely understand what it is you want to do, but extrapolating from "adding error Location details to source errors", it sounds like you want to make it easier for the programmer. If that's the case, it seems like you'd want something syntactically like std::env::var("MISSING_VARIABLE").add_location()?;
std::io::read('/foo').add_location()?;
foo.try_into().add_location()?; However, I'd highly discourage such a path because the error message is basically useless to the person running the program:
One of the biggest points of SNAFU is that you add context to the error that makes it sensical to the caller (which eventually is the person running the program). 99.9% of the time, errors I create do not use All that being said, if you wanted to get some syntax close to what you propose, you could create your own macro (I think either procedural or declarative would work) that does the transform for you. Your example doesn't have If you wanted to reduce the noise from |
Beta Was this translation helpful? Give feedback.
I can't immediately think of a great way to do this with SNAFU.
I'm not sure I completely understand what it is you want to do, but extrapolating from "adding error Location details to source errors", it sounds like you want to make it easier for the programmer. If that's the case, it seems like you'd want something syntactically like
However, I'd highly discourage such a path because the error message is basically useless to the person running the program:
One of the biggest points of SNAFU is that you add …