From f894b396b3eee4310fe5f6f7202b32d22d293c4e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 7 Nov 2026 14:46:03 +0100 Subject: refactor(yt_dlp/python_error): Add context to every `wrap_exc` call Otherwise it is hard to debug where the wrapped exception was thrown. --- crates/yt_dlp/src/python_error.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'crates/yt_dlp/src/python_error.rs') 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: Sized { - fn wrap_exc(self, py: Python<'_>) -> Result; + fn wrap_exc(self, py: Python<'_>, context: &'static str) -> Result; } impl IntoPythonError for Result { - fn wrap_exc(self, py: Python<'_>) -> Result { - self.map_err(|exc| PythonError::from_exception(py, &exc)) + fn wrap_exc(self, py: Python<'_>, context: &'static str) -> Result { + 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, + } } } -- cgit v1.3.1