ciphercore_base/
errors.rs1use std::sync::Arc;
2
3#[doc(hidden)]
4#[macro_export]
5macro_rules! runtime_error {
6 ($($x:tt)*) => {
7 $crate::errors::Error::new(anyhow::anyhow!($($x)*), true)
8 };
9}
10
11#[derive(Clone)]
12pub struct Error {
13 inner: Arc<anyhow::Error>,
15 pub can_retry: bool,
16}
17
18impl Error {
19 pub fn new(err: anyhow::Error, can_retry: bool) -> Self {
20 Self {
21 inner: Arc::new(err),
22 can_retry,
23 }
24 }
25}
26
27pub type Result<T> = std::result::Result<T, Error>;
28
29impl std::fmt::Debug for Error {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 std::fmt::Debug::fmt(&self.inner, f)
32 }
33}
34
35impl std::fmt::Display for Error {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 std::fmt::Display::fmt(&self.inner, f)
38 }
39}
40
41#[cfg(feature = "tonic-errors")]
42impl From<Error> for tonic::Status {
43 fn from(err: Error) -> Self {
44 tonic::Status::new(tonic::Code::Internal, err.inner.to_string())
46 }
47}
48
49#[cfg(feature = "py-binding")]
50impl std::convert::From<Error> for pyo3::PyErr {
51 fn from(err: Error) -> pyo3::PyErr {
52 pyo3::exceptions::PyRuntimeError::new_err(err.to_string())
53 }
54}
55
56impl<E> From<E> for Error
57where
58 E: Into<anyhow::Error>,
59{
60 fn from(error: E) -> Self {
61 Self::new(error.into(), true)
64 }
65}
66
67pub fn poisoned_mutex<E>(_: E) -> Error {
69 runtime_error!("Poisoned mutex")
70}