Extended fork of https://gitlab.com/the-SSD/error-backtrace (ideally gets folded back).
- Rust 100%
| .cargo | Add cargo config | |
| examples | 0.3.0 | |
| src | Remove debug print | |
| .gitignore | initual implementation | |
| .gitlab-ci.yml |
Configure SAST in .gitlab-ci.yml, creating this file if it does not already exist
|
|
| Cargo.toml | Bump version | |
| LICENSE | Add LICENSE | |
| README.md | Add wrapped error conversions | |
Error Backtrace
This is a simple crate to debug where your errors are comming from. With the smallest possiable amount of code.
Usage
cargo add error-backtrace- Add
use error_backtrace::{Result, ResultBacktrace};where you useResultand.backtrace()to backtrace them - And add
.into()where the error is created. (Compiler will help you with this) - Where you read errors use
error.inneror*error - Use
.unwraponResultto print the backtrace.
Example:
useerror_backtrace::{Result,ResultBacktrace};#[derive(Debug)]struct Error;fn main()-> Result<(),Error>{maybe_error().backtrace()?;Ok(())}fn maybe_error()-> Result<(),Error>{error_source()?;Ok(())}fn error_source()-> Result<(),Error>{Err(Error{}.into())}Actual real world backtrace!
thread 'main' panicked at crates/vidi-tools/src/bin/camera_getframes.rs:39:65:
called `Result::unwrap()` on an `Err` value: Io(Os { code: 22, kind: InvalidInput, message: "Invalid argument" })
Backtrace:
0: <core::result::Result<T,F> as core::ops::try_trait::FromResidual<core::result::Result<core::convert::Infallible,E>>>::from_residual
at /rustc/9fc6b43126469e3858e2fe86cafb4f0fd5068869/library/core/src/result.rs:2009:27
1: vidi::pipelines::AcquiredCamera::configure
at /home/rustism/libobscura/crates/vidi/src/pipelines/mod.rs:252:17
2: vidi::pipelines::AcquiredCamera::configure_pipeline
at /home/rustism/libobscura/crates/vidi/src/pipelines/mod.rs:282:9
3: vidi::pipelines::AcquiredCamera::start
at /home/rustism/libobscura/crates/vidi/src/pipelines/mod.rs:289:9
4: camera_getframes::main::{{closure}}
at /home/rustism/libobscura/crates/vidi-tools/src/bin/camera_getframes.rs:39:34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
The message is not wrong, you can get a backtrace using RUST_BACKTRACE=1. But that backtrace only goes down to the .unwrap() call. Meanwhile, this gives you the place where you called .backtrace()
Boxed errors
Example:
useerror_backtrace::{Result,ResultBacktrace,GenericError};useio;usestd::error::Error;fn literal_to_generic()-> std::result::Result<(),GenericError>{lete=io::Error::other("simple");Err(GenericError(Box::new(e)))}fn literal_to_generic_traced()-> Result<(),GenericError>{lete=io::Error::other("simple");Ok(Err(GenericError(Box::new(e)))?)}Converting between traced errors
The errors are wrapped inside Backtraced<E>, making Result::map_err a bit difficult to use. But there are map_trace methods in ResultBacktrace to make things good again.
usestd::io;useerror_backtrace:{Result,ResultBacktrace};struct MyError(io::Error);implFrom<io::Error>forMyError{fn from(value: io::Error)-> Self{Self(value)}}fn convert(res: Result<(),io::Error>)-> Result<(),MyError>{res.map_trace(MyError::from)}fn convert_into(res: Result<(),io::Error>)-> Result<(),MyError>{res.map_trace_into()}