Better failure_to_pyerr

This commit is contained in:
Eric Eastwood
2026-06-24 17:53:28 -05:00
parent b5ea2f7154
commit 5ce6b8a307
+10 -7
View File
@@ -293,20 +293,23 @@ where
}
}
/// Convert a Twisted `Failure` (as passed to an errback) into a [`PyErr`].
/// Convert a Twisted `Failure` (as passed to an Deferred errback) into a [`PyErr`].
///
/// A `Failure` carries the original exception instance in its `.value`
/// attribute, which we re-raise so callers see the real error. If that can't be
/// reached, fall back to the `Failure`'s textual representation.
/// A Twisted `Failure` carries the original exception instance in its `.value`
/// attribute, which we re-raise so callers see the real error. If the `Failure` is
/// mangled, we fallback to raising a generic [`PyRuntimeError`] explaining what we saw
/// instead.
fn failure_to_pyerr(failure: &Bound<'_, PyAny>) -> PyErr {
match failure.getattr(intern!(failure.py(), "value")) {
Ok(value) => PyErr::from_value(value),
Err(_) => PyRuntimeError::new_err(
Err(_) => PyRuntimeError::new_err(format!(
"Expected Python object passed here to be a Twisted `Failure` with a `value` attribute \
but saw something else: {}",
failure
.str()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|_| "<unknown failure>".to_owned()),
),
.unwrap_or_else(|_| "<failed to stringify Python object>".to_owned()),
)),
}
}