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 #[error(transparent)]
33 SerdeJsonError(#[from] serde_json::Error),
34}
35
36impl<'a> From<pyo3::DowncastError<'a, 'a>> for UtilError {
37 fn from(err: pyo3::DowncastError) -> Self {
38 UtilError::DowncastError(err.to_string())
39 }
40}
41
42impl From<UtilError> for PyErr {
43 fn from(err: UtilError) -> PyErr {
44 let msg = err.to_string();
45 error!("{}", msg);
46 PyRuntimeError::new_err(msg)
47 }
48}
49
50impl From<PyErr> for UtilError {
51 fn from(err: PyErr) -> UtilError {
52 UtilError::PyError(err.to_string())
53 }
54}