mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-30 14:32:13 +00:00
fix: ok_or_eyre
not using track_caller
(#140)
`ok_or_eyre` did not use `track_caller`, which meant the error location was incorrect compared to the equivalent anyhow::context feature
This commit is contained in:
parent
770ac3fa14
commit
53ec78d8c0
@ -2,6 +2,7 @@ use crate::OptionExt;
|
|||||||
use core::fmt::{Debug, Display};
|
use core::fmt::{Debug, Display};
|
||||||
|
|
||||||
impl<T> OptionExt<T> for Option<T> {
|
impl<T> OptionExt<T> for Option<T> {
|
||||||
|
#[track_caller]
|
||||||
fn ok_or_eyre<M>(self, message: M) -> crate::Result<T>
|
fn ok_or_eyre<M>(self, message: M) -> crate::Result<T>
|
||||||
where
|
where
|
||||||
M: Debug + Display + Send + Sync + 'static,
|
M: Debug + Display + Send + Sync + 'static,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::panic::Location;
|
use std::panic::Location;
|
||||||
|
|
||||||
use eyre::WrapErr;
|
use eyre::{OptionExt as _, WrapErr};
|
||||||
|
|
||||||
struct LocationHandler {
|
struct LocationHandler {
|
||||||
actual: Option<&'static str>,
|
actual: Option<&'static str>,
|
||||||
@ -83,6 +83,19 @@ fn test_wrap_err_with() {
|
|||||||
println!("{:?}", err);
|
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]
|
#[test]
|
||||||
fn test_context() {
|
fn test_context() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user