miette icon indicating copy to clipboard operation
miette copied to clipboard

`track_caller()` not being called when using custom `ReportHandler`

Open ArchieAtkinson opened this issue 10 months ago • 0 comments

Hey!

I'm having trouble getting caller location tracking to work with a custom ReportHandler in miette.

I want to output the caller's location whenever an error occurs, similar to what's shown in the test_location.rs tests. However, my track_caller() implementation doesn't seem to be getting called at all.

Here's a minimal example that demonstrates the issue:

#[track_caller]
fn testing() -> miette::Result<String> {
    std::fs::read_to_string("totally_fake_path").into_diagnostic()
}

struct LocationHandler {
    actual: Option<&'static str>,
}

impl LocationHandler {
    fn new() -> Self {
        LocationHandler { actual: None }
    }
}

impl miette::ReportHandler for LocationHandler {
    fn debug(&self, _error: &dyn Diagnostic, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        eprintln!("Error File: {:?}", self.actual.unwrap());
        Ok(())
    }

    fn track_caller(&mut self, location: &'static Location<'static>) {
        self.actual = Some(location.file());
    }
}

#[test]
#[track_caller]
fn test_track_caller() -> miette::Result<()> {
    let _ = miette::set_hook(Box::new(|_e| -> Box<dyn ReportHandler> {
        Box::new(LocationHandler::new())
    }));

    testing()?;

    Ok(())
}

What's happening: I've created a simple LocationHandler that should track the caller's location The test tries to read a non-existent file to trigger an error Instead of getting the caller's location, it panics when trying to unwrap self.actual because track_caller() was never called.

Expected behaviour: The track_caller() method should be called when the error occurs

Current behaviour: track_caller() is never called

I'm using miette version 7.5.0. What might I be doing wrong here?

Would be great if someone could point out if I'm missing something obvious or if this is potentially a bug.

Cheers

ArchieAtkinson avatar Feb 14 '25 19:02 ArchieAtkinson