1use pyo3::exceptions::PyRuntimeError;
2use pyo3::PyErr;
3use pythonize::PythonizeError;
4use thiserror::Error;
5use tracing::error;
6
7#[derive(Error, Debug)]
8pub enum UtilError {
9 #[error("Error serializing data")]
10 SerializationError,
11
12 #[error("Error getting parent path")]
13 GetParentPathError,
14
15 #[error("Failed to create directory")]
16 CreateDirectoryError,
17
18 #[error("Failed to write to file")]
19 WriteError,
20
21 #[error("Invalid number")]
22 InvalidNumber,
23
24 #[error("Root must be an object")]
25 RootMustBeObjectError,
26
27 #[error("{0}")]
28 PyError(String),
29
30 #[error("Failed to downcast Python object: {0}")]
31 DowncastError(String),
32
33 #[error(transparent)]
34 SerdeJsonError(#[from] serde_json::Error),
35
36 #[error("Failed to check if the context is a Pydantic BaseModel. Error: {0}")]
37 FailedToCheckPydanticModel(String),
38}
39
40impl From<PythonizeError> for UtilError {
41 fn from(err: PythonizeError) -> Self {
42 UtilError::PyError(err.to_string())
43 }
44}
45
46impl<'a, 'py> From<pyo3::CastError<'a, 'py>> for UtilError {
47 fn from(err: pyo3::CastError) -> Self {
48 UtilError::DowncastError(err.to_string())
49 }
50}
51
52impl From<UtilError> for PyErr {
53 fn from(err: UtilError) -> PyErr {
54 let msg = err.to_string();
55 error!("{}", msg);
56 PyRuntimeError::new_err(msg)
57 }
58}
59
60impl From<PyErr> for UtilError {
61 fn from(err: PyErr) -> UtilError {
62 UtilError::PyError(err.to_string())
63 }
64}