Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track_caller() not being called when using custom ReportHandler #422

Open
ArchieAtkinson opened this issue Feb 14, 2025 · 0 comments
Open

Comments

@ArchieAtkinson
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant