diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-07 14:46:03 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-07 14:46:03 +0100 |
| commit | f894b396b3eee4310fe5f6f7202b32d22d293c4e (patch) | |
| tree | 51cc15b24da4b94ed5f0776d9d5305c009cba17e /crates/yt_dlp/src/python_error.rs | |
| parent | fix(yt/db/get/txn_log): Order by `ROWID` instead of `timestamp` (diff) | |
| download | yt-f894b396b3eee4310fe5f6f7202b32d22d293c4e.zip | |
refactor(yt_dlp/python_error): Add context to every `wrap_exc` call
Otherwise it is hard to debug where the wrapped exception was thrown.
Diffstat (limited to 'crates/yt_dlp/src/python_error.rs')
| -rw-r--r-- | crates/yt_dlp/src/python_error.rs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/crates/yt_dlp/src/python_error.rs b/crates/yt_dlp/src/python_error.rs index 0c442b3..c46677b 100644 --- a/crates/yt_dlp/src/python_error.rs +++ b/crates/yt_dlp/src/python_error.rs @@ -14,28 +14,38 @@ use log::{Level, debug, log_enabled}; use pyo3::{PyErr, Python, types::PyTracebackMethods}; #[derive(thiserror::Error, Debug)] -pub struct PythonError(pub String); +pub struct PythonError { + pub python_error_message: String, + context: &'static str, +} pub(crate) trait IntoPythonError<T>: Sized { - fn wrap_exc(self, py: Python<'_>) -> Result<T, PythonError>; + fn wrap_exc(self, py: Python<'_>, context: &'static str) -> Result<T, PythonError>; } impl<T> IntoPythonError<T> for Result<T, PyErr> { - fn wrap_exc(self, py: Python<'_>) -> Result<T, PythonError> { - self.map_err(|exc| PythonError::from_exception(py, &exc)) + fn wrap_exc(self, py: Python<'_>, context: &'static str) -> Result<T, PythonError> { + self.map_err(|exc| PythonError::from_exception(py, &exc, context)) } } impl Display for PythonError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Python threw an exception: {}", self.0) + write!( + f, + "Python threw an exception: {} (while {})", + self.python_error_message, self.context + ) } } impl PythonError { - pub(super) fn from_exception(py: Python<'_>, exc: &PyErr) -> Self { + pub(super) fn from_exception(py: Python<'_>, exc: &PyErr, context: &'static str) -> Self { let buffer = process_exception(py, exc); - Self(buffer) + Self { + python_error_message: buffer, + context, + } } } |
