Skip to content

Commit

Permalink
python: Include line number in state error
Browse files Browse the repository at this point in the history
StateMismatch errors can be quite hard to understand, this gives at
least a pointer to where it comes from.
  • Loading branch information
chrysn committed Feb 3, 2025
1 parent 8949331 commit 3311f91
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lakers-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ impl std::fmt::Display for StateMismatch {
}
}
impl From<StateMismatch> for PyErr {
#[track_caller]
fn from(err: StateMismatch) -> PyErr {
pyo3::exceptions::PyRuntimeError::new_err(err.to_string())
let location = std::panic::Location::caller();
// It would be nice to inject something more idiomatic on the Python side, eg. setting a
// cause with a Rust file and line number, but to create such an object we'd need the GIL,
// and that'd required doing a lot of things custom, eg. by creating a custom class where
// we pass that extra info in extra arguments, or re-implementing PyErr's lazy state (which
// we can't hook into because that's private) -- having the location in the text is the
// second best option.
pyo3::exceptions::PyRuntimeError::new_err(format!("{} (internally {})", err, location))
}
}

Expand Down

0 comments on commit 3311f91

Please sign in to comment.