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

backtrace() should prefer the source's backtrace if it has one #7

Closed
dtolnay opened this issue Oct 10, 2019 · 3 comments · Fixed by #28
Closed

backtrace() should prefer the source's backtrace if it has one #7

dtolnay opened this issue Oct 10, 2019 · 3 comments · Fixed by #28

Comments

@dtolnay
Copy link
Owner

dtolnay commented Oct 10, 2019

#[derive(Error, Debug)]
pub struct Error {
    #[source]
    source: anyhow::Error,
    backtrace: Backtrace,
}

Currently the generated backtrace() method would just return Some(&self.backtrace). Instead it should be returning Some(self.source.backtrace().unwrap_or(&self.backtrace)). The lower-level error will tend to have a deeper more relevant backtrace.

Note: I am not confident about this. Which behavior is the right one?

Separately, we should return source backtraces if self doesn't contain its own backtrace, which we don't currently do. self.source.backtrace() in the following case.

#[derive(Error, Debug)]
pub struct Error {
    #[source]
    source: anyhow::Error,
}
@Jezza
Copy link

Jezza commented Oct 10, 2019

Yeah, source's backtrace should be more relevant.

Maybe a good idea would be an explicit #[backtrace].
Only needed if it's ambiguous, or even better, default to the source's backtrace, but emit a warning or something.
That way nothing breaks, the users can still select the one they want, and we get better default backtraces.

@TyPR124
Copy link

TyPR124 commented Oct 11, 2019

What if the user launches a thread that might return an error, and they include that error as a source on the main thread? Would that produce two completely separate (but potentially equally important) backtraces?

If that is correct, I feel like hiding the outer backtrace may be a bad idea, especially as the inner backtrace would still be accessible through source.

@mathstuf
Copy link
Contributor

An explicit #[backtrace] could elevate its precedence over the source backtrace.

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.

4 participants