#thiserror backtraces

21 messages · Page 1 of 1 (latest)

coarse gale
#

eyre provides awesome backtraces, like this: https://raw.githubusercontent.com/yaahc/color-eyre/master/pictures/custom_section.png
This is helpful because it doesn't just track what the underlying error was, but where it was from.

however, eyre requires heap allocation and trait objects, which I want to avoid on my embedded device.

Instead, I'd like to use thiserror. But as best as I can tell, thiserror doesn't track backtraces - is that accurate?
I saw #[backtrace] but I want every error to have a backtrace - does this mean every variant of my error enum needs to be a struct with source and backtrace?

Perhaps there's another library that would be better for me to use?

unborn flare
#

as far as I can tell, capturing backtraces will always require allocations, because there's basically some sort of Vec<Frame> somewhere

#

would you be happy with just the location where it happened rather than an entire backtrace

#

?play ```rust
use std::fmt;
use std::panic::Location;

struct MyError(&'static Location<'static>);

impl fmt::Debug for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Oh no something went horribly wrong at: {}", self.0)
}
}

impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Oh no something went horribly wrong at: {}", self.0)
}
}

impl<E: std::error::Error> From<E> for MyError {
#[track_caller]
fn from(_: E) -> Self {
MyError(Location::caller())
}
}

fn main() -> Result<(), MyError> {
let result = "r43".parse();
let i: i32 = result?;
Ok(())
}

lone lakeBOT
#
     Running `target/debug/playground`
Error: Oh no something went horribly wrong at: src/main.rs:27:18
coarse gale
#

That seems like a decent approach

#

but I take it that this cannot be done with thiserror?

coarse gale
#

looks like the backtrace crate requires alloc so I guess backtraces aren't even the right approach here

unborn flare
#

It probably is possible

#

I am not aware of how...uh "opinionated" thiserror is

#

Otherwise just dont use thiserror, its not so bad

coarse gale
#

what should I do if I cant use thiserror or eyre? I've never done that

#

(and I can't panic)

unborn flare
#

Write out your own error type

#

Is std available at all?

coarse gale
#

nope

#

just core

#

I have alloc too but using it every time I create an error would probably be out of the question

unborn flare
#

core:;error is nightly, so you will need to use nightly for that