Skip to content

Commit

Permalink
fix: ok_or_eyre not using track_caller (#140)
Browse files Browse the repository at this point in the history
`ok_or_eyre` did not use `track_caller`, which meant the error location
was incorrect compared to the equivalent anyhow::context feature
  • Loading branch information
ten3roberts committed Dec 30, 2023
1 parent 770ac3f commit 53ec78d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions eyre/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::OptionExt;
use core::fmt::{Debug, Display};

impl<T> OptionExt<T> for Option<T> {
#[track_caller]
fn ok_or_eyre<M>(self, message: M) -> crate::Result<T>
where
M: Debug + Display + Send + Sync + 'static,
Expand Down
15 changes: 14 additions & 1 deletion eyre/tests/test_location.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::panic::Location;

use eyre::WrapErr;
use eyre::{OptionExt as _, WrapErr};

struct LocationHandler {
actual: Option<&'static str>,
Expand Down Expand Up @@ -83,6 +83,19 @@ fn test_wrap_err_with() {
println!("{:?}", err);
}

#[test]
fn test_option_ok_or_eyre() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));

let err = None::<()>.ok_or_eyre("oopsie").unwrap_err();

// should panic if the location isn't in our crate
println!("{:?}", err);
}

#[test]
fn test_context() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand Down

0 comments on commit 53ec78d

Please sign in to comment.