Skip to content
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

Support for tracing with Std::panic::Location (Feature Request) #142

Open
mhgolkar opened this issue Jul 15, 2021 · 2 comments · May be fixed by #291
Open

Support for tracing with Std::panic::Location (Feature Request) #142

mhgolkar opened this issue Jul 15, 2021 · 2 comments · May be fixed by #291

Comments

@mhgolkar
Copy link

Hello there

It's sometime useful to simply trace the location of an error's origin, through nested callers and micro managed errors.
Compared to already existing Backtrace, it might be more concise, and thanks to std::panic::Location and #[track_caller] being stabilized it wouldn't need nightly.

Thanks for the great crate

@Allen-Webb
Copy link

I also think some kind of backtrace chaining through source errors is really needed for thiserror.

@ewoolsey
Copy link

Anyone know if there are plans to implement this? It looks like it should be pretty easy. A basic implementation looks like this

// New error type encapsulating the original error and location data.
#[derive(Debug, Clone)]
struct LocatedError<E: Error + 'static> {
    inner: E,
    location: &'static Location<'static>,
}

impl<E: Error + 'static> Error for LocatedError<E> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.inner)
    }
}

impl<E: Error + 'static> std::fmt::Display for LocatedError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}, {}", self.inner, self.location)
    }
}

impl From<std::io::Error> for LocatedError<std::io::Error> {
    // The magic happens here
    #[track_caller]
    fn from(err: std::io::Error) -> Self {
        LocatedError {
            inner: err,
            location: std::panic::Location::caller(),
        }
    }
}

To me this feature is basically begging to be included in thiserror.

@onlycs onlycs linked a pull request Mar 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants