1
8
Fork
You've already forked miette
0

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

Open
opened 2025年02月14日 20:08:41 +01:00 by ArchieAtkinson · 0 comments
ArchieAtkinson commented 2025年02月14日 20:08:41 +01:00 (Migrated from github.com)
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<&'staticstr>,}implLocationHandler{fn new()-> Self{LocationHandler{actual: None }}}implmiette::ReportHandlerforLocationHandler{fn debug(&self,_error: &dynDiagnostic,_f: &mutstd::fmt::Formatter<'_>)-> std::fmt::Result{eprintln!("Error File: {:?}",self.actual.unwrap());Ok(())}fn track_caller(&mutself,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<dynReportHandler>{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

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: ```rust #[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 in to join this conversation.
No Branch/Tag specified
main
zkat/typed-report
v7.6.0
miette-derive-v7.6.0
v7.4.0
miette-derive-v7.4.0
v7.3.0
miette-derive-v7.3.0
v7.2.0
miette-derive-v7.2.0
miette-derive-v7.1.0
v7.1.0
miette-derive-v7.0.0
v7.0.0
miette-derive-v6.0.1
v6.0.1
miette-derive-v6.0.0
v6.0.0
miette-derive-v5.10.0
v5.10.0
miette-derive-v5.9.0
v5.9.0
miette-derive-v5.8.0
v5.8.0
miette-derive-v5.7.0
v5.7.0
miette-derive-v5.6.0
v5.6.0
miette-derive-v5.5.0
v5.5.0
miette-derive-v5.4.1
v5.4.1
miette-derive-v5.4.0
v5.4.0
miette-derive-v5.3.1
v5.3.1
miette-derive-v5.3.0
v5.3.0
v5.2.0
miette-derive-v5.2.0
miette-derive-v5.1.1
v5.1.1
miette-derive-v5.1.0
v5.1.0
miette-derive-v5.0.0
v5.0.0
miette-derive-v4.7.1
v4.7.1
miette-derive-v4.7.0
v4.7.0
miette-derive-v4.6.0
v4.6.0
miette-derive-v4.5.0
v4.5.0
miette-derive-v4.4.0
v4.4.0
miette-derive-v4.3.0
v4.3.0
v4.2.1
v4.2.0
v4.1.0
v4.0.1
miette-derive-v4.0.0
v4.0.0
miette-derive-v3.3.0
v3.3.0
miette-derive-v3.2.0
v3.2.0
v3.1.0
v3.0.1
v3.0.0
v3.0.0-beta.0
v3.0.0-alpha.0
miette-derive-v2.2.0
v2.2.0
miette-derive-v2.1.2
v2.1.2
v2.1.1
v2.1.0
v2.0.0
miette-derive-v2.0.0
v1.1.0
1.0.1
1.0.0
1.0.0-beta.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.1
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
zkat/miette#422
Reference in a new issue
zkat/miette
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?