1
0
Fork
You've already forked error-backtrace
0
Extended fork of https://gitlab.com/the-SSD/error-backtrace (ideally gets folded back).
  • Rust 100%
2025年02月20日 03:36:31 +00:00
.cargo Add cargo config 2024年12月31日 16:22:31 +00:00
examples 0.3.0 2024年08月28日 17:56:18 +02:00
src Remove debug print 2025年02月20日 03:35:55 +00:00
.gitignore initual implementation 2024年08月23日 11:48:55 +02:00
.gitlab-ci.yml Configure SAST in .gitlab-ci.yml, creating this file if it does not already exist 2024年08月23日 09:39:15 +00:00
Cargo.toml Bump version 2025年02月20日 03:36:31 +00:00
LICENSE Add LICENSE 2024年08月23日 09:49:49 +00:00
README.md Add wrapped error conversions 2025年02月20日 00:55:58 +00:00

Error Backtrace

This is a simple crate to debug where your errors are comming from. With the smallest possiable amount of code.

Usage

  1. cargo add error-backtrace
  2. Add use error_backtrace::{Result, ResultBacktrace}; where you use Result and .backtrace() to backtrace them
  3. And add .into() where the error is created. (Compiler will help you with this)
  4. Where you read errors use error.inner or *error
  5. Use .unwrap on Result to 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()}