aboutsummaryrefslogtreecommitdiffstats
path: root/crates/yt_dlp/src/python_error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/yt_dlp/src/python_error.rs')
-rw-r--r--crates/yt_dlp/src/python_error.rs24
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,
+ }
}
}