Enhanced utility function to convert any error to PyErr with context
(error: E)
| 188 | |
| 189 | /// Enhanced utility function to convert any error to PyErr with context |
| 190 | pub(crate) fn to_py_runtime_error<E: std::fmt::Display>(error: E) -> PyErr { |
| 191 | let error_msg = error.to_string(); |
| 192 | warn!( |
| 193 | "Converting runtime error to Python exception: {}", |
| 194 | error_msg |
| 195 | ); |
| 196 | |
| 197 | // Check for common error patterns and map to appropriate exceptions |
| 198 | if error_msg.contains("timeout") || error_msg.contains("deadline") { |
| 199 | PyErr::new::<pyo3::exceptions::PyTimeoutError, _>(error_msg) |
| 200 | } else if error_msg.contains("permission") || error_msg.contains("unauthorized") { |
| 201 | PyErr::new::<pyo3::exceptions::PyPermissionError, _>(error_msg) |
| 202 | } else if error_msg.contains("not found") || error_msg.contains("missing") { |
| 203 | PyErr::new::<pyo3::exceptions::PyFileNotFoundError, _>(error_msg) |
| 204 | } else if error_msg.contains("invalid") || error_msg.contains("malformed") { |
| 205 | PyErr::new::<pyo3::exceptions::PyValueError, _>(error_msg) |
| 206 | } else if error_msg.contains("connection") || error_msg.contains("network") { |
| 207 | PyErr::new::<pyo3::exceptions::PyConnectionError, _>(error_msg) |
| 208 | } else { |
| 209 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(error_msg) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /// Create validation error with field and value context |
| 214 | pub(crate) fn validation_error(field: &str, value: Option<&str>, message: &str) -> PyErr { |