How to trace the cause of an error result?

前端 未结 2 1492
误落风尘
误落风尘 2021-01-11 10:53

When writing code that uses Result type, you may want different behavior for users and developers.

  • When writing an application that gracefully han
相关标签:
2条回答
  • 2021-01-11 11:29

    A result by itself doesn't have any backtrace information, but you can add it to custom error types.

    The error_chain crate is an example which generates an error type for you, for which you get backtrace generation for free when the RUST_BACKTRACE environment variable is set.

    You could also use the backtrace library directly and do it yourself.

    0 讨论(0)
  • 2021-01-11 11:33

    If you use anyhow you can get this for free! The catch is you need to use nightly and enable an environment variable:

    RUST_BACKTRACE=1 cargo +nightly run
    

    This is the tracking issue for stabilisation, and a PR to stabilise it. Looks like there is some disagreement about whether it needs to work in no_std before being stabilised or something like that.

    0 讨论(0)
提交回复
热议问题