diff options
Diffstat (limited to 'crates/yt_dlp/src')
| -rw-r--r-- | crates/yt_dlp/src/lib.rs | 42 | ||||
| -rw-r--r-- | crates/yt_dlp/src/options.rs | 48 | ||||
| -rw-r--r-- | crates/yt_dlp/src/python_error.rs | 24 |
3 files changed, 72 insertions, 42 deletions
diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs index abe766d..22c9185 100644 --- a/crates/yt_dlp/src/lib.rs +++ b/crates/yt_dlp/src/lib.rs @@ -93,7 +93,7 @@ macro_rules! py_kw_args { let dict = PyDict::new($py); $( - dict.set_item(stringify!($kw_arg_name), $kw_arg_val).wrap_exc($py)?; + dict.set_item(stringify!($kw_arg_name), $kw_arg_val).wrap_exc($py, "constructing python kw args")?; )* Some(dict) @@ -118,13 +118,13 @@ impl YoutubeDL { Python::attach(|py| { let yt_dlp = py .import(intern!(py, "yt_dlp")) - .wrap_exc(py)? + .wrap_exc(py, "importing yt_dlp")? .getattr(intern!(py, "version")) - .wrap_exc(py)? + .wrap_exc(py, "accessing 'version'")? .getattr(intern!(py, "__version__")) - .wrap_exc(py)? + .wrap_exc(py, "accessing '__version__'")? .extract() - .wrap_exc(py)?; + .wrap_exc(py, "extracting version as str")?; let python = py.version(); @@ -194,14 +194,14 @@ impl YoutubeDL { .inner .bind(py) .getattr(intern!(py, "extract_info")) - .wrap_exc(py)?; + .wrap_exc(py, "extracting extract_info as function")?; let result = inner .call( (url.to_string(),), py_kw_args!(py => download = download, process = process), ) - .wrap_exc(py)? + .wrap_exc(py, "calling extract_info")? .cast_into::<PyDict>() .expect("This is a dict"); @@ -218,14 +218,16 @@ impl YoutubeDL { let mut out = vec![]; for output in generator { - out.push(output.wrap_exc(py)?); + out.push(output.wrap_exc(py, "processing playlist item")?); if out.len() == max_backlog { break; } } - result.set_item(intern!(py, "entries"), out).wrap_exc(py)?; + result + .set_item(intern!(py, "entries"), out) + .wrap_exc(py, "setting playlist 'entries'")?; } else { // Probably some sort of paged list (`OnDemand` or otherwise) let max_backlog = json_try_get!(self.options, "playlistend", as_u64) @@ -233,15 +235,17 @@ impl YoutubeDL { usize::try_from(playlistend).expect("Should work") }); - let next = generator.getattr(intern!(py, "getslice")).wrap_exc(py)?; + let next = generator + .getattr(intern!(py, "getslice")) + .wrap_exc(py, "getting 'getslice' attribute")?; let output = next .call((), py_kw_args!(py => start = 0, end = max_backlog)) - .wrap_exc(py)?; + .wrap_exc(py, "calling 'getslice'")?; result .set_item(intern!(py, "entries"), output) - .wrap_exc(py)?; + .wrap_exc(py, "setting 'entries' key")?; } } @@ -274,14 +278,14 @@ impl YoutubeDL { .inner .bind(py) .getattr(intern!(py, "process_ie_result")) - .wrap_exc(py)?; + .wrap_exc(py, "getting process_ie_result")?; let result = inner .call( (json_loads(ie_result, py),), py_kw_args!(py => download = download), ) - .wrap_exc(py)? + .wrap_exc(py, "calling process_ie_result")? .cast_into::<PyDict>() .expect("This is a dict"); @@ -303,9 +307,9 @@ impl YoutubeDL { .inner .bind(py) .getattr(intern!(py, "close")) - .wrap_exc(py)?; + .wrap_exc(py, "getting 'close'")?; - inner.call0().wrap_exc(py)?; + inner.call0().wrap_exc(py, "calling 'close'")?; Ok(()) }) @@ -320,9 +324,11 @@ impl YoutubeDL { .inner .bind(py) .getattr(intern!(py, "sanitize_info")) - .wrap_exc(py)?; + .wrap_exc(py, "getting 'sanitize_info'")?; - let value = sanitize.call((info,), None).wrap_exc(py)?; + let value = sanitize + .call((info,), None) + .wrap_exc(py, "calling sanitize info")?; let result = value.cast::<PyDict>().expect("This should stay a dict"); diff --git a/crates/yt_dlp/src/options.rs b/crates/yt_dlp/src/options.rs index a87473d..e3373b2 100644 --- a/crates/yt_dlp/src/options.rs +++ b/crates/yt_dlp/src/options.rs @@ -145,17 +145,20 @@ signal.signal(signal.SIGINT, signal.SIG_DFL) { // Setup the progress hook if let Some(ph) = options.progress_hook { - opts.set_item(intern!(py, "progress_hooks"), vec![ph(py).wrap_exc(py)?]) - .wrap_exc(py)?; + opts.set_item( + intern!(py, "progress_hooks"), + vec![ph(py).wrap_exc(py, "not having context")?], + ) + .wrap_exc(py, "not having context")?; } // Setup the post_processor hook if let Some(ph) = options.post_processor_hook { opts.set_item( intern!(py, "postprocessor_hooks"), - vec![ph(py).wrap_exc(py)?], + vec![ph(py).wrap_exc(py, "not having context")?], ) - .wrap_exc(py)?; + .wrap_exc(py, "not having context")?; } } @@ -163,47 +166,58 @@ signal.signal(signal.SIGINT, signal.SIG_DFL) // Unconditionally set a logger. // Otherwise, yt_dlp will log to stderr. - let ytdl_logger = setup_logging(py, "yt_dlp").wrap_exc(py)?; + let ytdl_logger = setup_logging(py, "yt_dlp").wrap_exc(py, "not having context")?; opts.set_item(intern!(py, "logger"), ytdl_logger) - .wrap_exc(py)?; + .wrap_exc(py, "not having context")?; } let inner = { - let p_params = opts.into_bound_py_any(py).wrap_exc(py)?; - let p_auto_init = true.into_bound_py_any(py).wrap_exc(py)?; + let p_params = opts + .into_bound_py_any(py) + .wrap_exc(py, "not having context")?; + let p_auto_init = true + .into_bound_py_any(py) + .wrap_exc(py, "not having context")?; py.import(intern!(py, "yt_dlp.YoutubeDL")) - .wrap_exc(py)? + .wrap_exc(py, "not having context")? .getattr(intern!(py, "YoutubeDL")) - .wrap_exc(py)? + .wrap_exc(py, "not having context")? .call1( PyTuple::new( py, [ - p_params.into_bound_py_any(py).wrap_exc(py)?, - p_auto_init.into_bound_py_any(py).wrap_exc(py)?, + p_params + .into_bound_py_any(py) + .wrap_exc(py, "not having context")?, + p_auto_init + .into_bound_py_any(py) + .wrap_exc(py, "not having context")?, ], ) - .wrap_exc(py)?, + .wrap_exc(py, "not having context")?, ) - .wrap_exc(py)? + .wrap_exc(py, "not having context")? }; { // Setup the post processors let add_post_processor_fun = inner .getattr(intern!(py, "add_post_processor")) - .wrap_exc(py)?; + .wrap_exc(py, "not having context")?; for pp in options.post_processors { add_post_processor_fun .call( - (pp(py).wrap_exc(py)?.into_bound_py_any(py).wrap_exc(py)?,), + (pp(py) + .wrap_exc(py, "not having context")? + .into_bound_py_any(py) + .wrap_exc(py, "not having context")?,), // "when" can take any value in yt_dlp.utils.POSTPROCESS_WHEN py_kw_args!(py => when = "pre_process"), ) - .wrap_exc(py)?; + .wrap_exc(py, "not having context")?; } } 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, + } } } |
