| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use pyo3::{ |
| 16 | exceptions::{PyIOError, PyNotImplementedError, PyOSError, PyRuntimeError, PyValueError}, |
| 17 | intern, |
| 18 | types::{PyAnyMethods, PyNone}, |
| 19 | PyErr, PyResult, Python, |
| 20 | }; |
| 21 | |
| 22 | use lancedb::error::Error as LanceError; |
| 23 | |
| 24 | pub trait PythonErrorExt<T> { |
| 25 | /// Convert to a python error based on the Lance error type |
| 26 | fn infer_error(self) -> PyResult<T>; |
| 27 | /// Convert to OSError |
| 28 | fn os_error(self) -> PyResult<T>; |
| 29 | /// Convert to RuntimeError |
| 30 | fn runtime_error(self) -> PyResult<T>; |
| 31 | /// Convert to ValueError |
| 32 | fn value_error(self) -> PyResult<T>; |
| 33 | } |
| 34 | |
| 35 | impl<T> PythonErrorExt<T> for std::result::Result<T, LanceError> { |
| 36 | fn infer_error(self) -> PyResult<T> { |
| 37 | match &self { |
| 38 | Ok(_) => Ok(self.unwrap()), |
| 39 | Err(err) => match err { |
| 40 | LanceError::InvalidInput { .. } |
| 41 | | LanceError::InvalidTableName { .. } |
| 42 | | LanceError::TableNotFound { .. } |
| 43 | | LanceError::Schema { .. } |
| 44 | | LanceError::TableAlreadyExists { .. } => self.value_error(), |
| 45 | LanceError::CreateDir { .. } => self.os_error(), |
| 46 | LanceError::ObjectStore { .. } => Err(PyIOError::new_err(err.to_string())), |
| 47 | LanceError::NotSupported { .. } => { |
| 48 | Err(PyNotImplementedError::new_err(err.to_string())) |
| 49 | } |
| 50 | LanceError::Http { |
| 51 | request_id, |
| 52 | source, |
| 53 | status_code, |
| 54 | } => Python::with_gil(|py| { |
| 55 | let message = err.to_string(); |
| 56 | let http_err_cls = py |
| 57 | .import_bound(intern!(py, "lancedb.remote.errors"))? |
| 58 | .getattr(intern!(py, "HttpError"))?; |
| 59 | let err = http_err_cls.call1(( |
| 60 | message, |
| 61 | request_id, |
| 62 | status_code.map(|s| s.as_u16()), |
| 63 | ))?; |
| 64 | |
| 65 | if let Some(cause) = source.source() { |
| 66 | // The HTTP error already includes the first cause. But |
| 67 | // we can add the rest of the chain if there is any more. |
| 68 | let cause_err = http_from_rust_error( |
| 69 | py, |
| 70 | cause, |
| 71 | request_id, |
| 72 | status_code.map(|s| s.as_u16()), |
| 73 | )?; |
| 74 | err.setattr(intern!(py, "__cause__"), cause_err)?; |
| 75 | } |
| 76 | |
| 77 | Err(PyErr::from_value_bound(err)) |
| 78 | }), |
| 79 | LanceError::Retry { |
| 80 | request_id, |
| 81 | request_failures, |
| 82 | max_request_failures, |
| 83 | connect_failures, |
| 84 | max_connect_failures, |
| 85 | read_failures, |
| 86 | max_read_failures, |
| 87 | source, |
| 88 | status_code, |
| 89 | } => Python::with_gil(|py| { |
| 90 | let cause_err = http_from_rust_error( |
| 91 | py, |
| 92 | source.as_ref(), |
| 93 | request_id, |
| 94 | status_code.map(|s| s.as_u16()), |
| 95 | )?; |
| 96 | |
| 97 | let message = err.to_string(); |
| 98 | let retry_error_cls = py |
| 99 | .import_bound(intern!(py, "lancedb.remote.errors"))? |
| 100 | .getattr("RetryError")?; |
| 101 | let err = retry_error_cls.call1(( |
| 102 | message, |
| 103 | request_id, |
| 104 | *request_failures, |
| 105 | *connect_failures, |
| 106 | *read_failures, |
| 107 | *max_request_failures, |
| 108 | *max_connect_failures, |
| 109 | *max_read_failures, |
| 110 | status_code.map(|s| s.as_u16()), |
| 111 | ))?; |
| 112 | |
| 113 | err.setattr(intern!(py, "__cause__"), cause_err)?; |
| 114 | Err(PyErr::from_value_bound(err)) |
| 115 | }), |
| 116 | _ => self.runtime_error(), |
| 117 | }, |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn os_error(self) -> PyResult<T> { |
| 122 | self.map_err(|err| PyOSError::new_err(err.to_string())) |
| 123 | } |
| 124 | |
| 125 | fn runtime_error(self) -> PyResult<T> { |
| 126 | self.map_err(|err| PyRuntimeError::new_err(err.to_string())) |
| 127 | } |
| 128 | |
| 129 | fn value_error(self) -> PyResult<T> { |
| 130 | self.map_err(|err| PyValueError::new_err(err.to_string())) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn http_from_rust_error( |
| 135 | py: Python<'_>, |
| 136 | err: &dyn std::error::Error, |
| 137 | request_id: &str, |
| 138 | status_code: Option<u16>, |
| 139 | ) -> PyResult<PyErr> { |
| 140 | let message = err.to_string(); |
| 141 | let http_err_cls = py.import("lancedb.remote.errors")?.getattr("HttpError")?; |
| 142 | let py_err = http_err_cls.call1((message, request_id, status_code))?; |
| 143 | |
| 144 | // Reset the traceback since it doesn't provide additional information. |
| 145 | let py_err = py_err.call_method1(intern!(py, "with_traceback"), (PyNone::get_bound(py),))?; |
| 146 | |
| 147 | if let Some(cause) = err.source() { |
| 148 | let cause_err = http_from_rust_error(py, cause, request_id, status_code)?; |
| 149 | py_err.setattr(intern!(py, "__cause__"), cause_err)?; |
| 150 | } |
| 151 | |
| 152 | Ok(PyErr::from_value(py_err)) |
| 153 | } |