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