otx_pool/
error.rs

1use otx_format::error::{OtxError, OtxFormatError};
2
3use anyhow::Result;
4use derive_more::Display;
5use jsonrpc_core::{Error, ErrorCode};
6use molecule::error::VerificationError;
7use serde::{Deserialize, Serialize};
8
9use std::fmt::Debug;
10
11pub type InnerResult<T> = Result<T, OtxRpcError>;
12
13#[derive(Debug, Display)]
14pub struct OtxRpcError(pub Box<dyn OtxError + Send>);
15
16impl From<OtxRpcError> for Error {
17    fn from(err: OtxRpcError) -> Error {
18        Error {
19            code: ErrorCode::ServerError(err.0.err_code()),
20            message: err.0.message(),
21            data: None,
22        }
23    }
24}
25
26impl From<OtxFormatError> for OtxRpcError {
27    fn from(err: OtxFormatError) -> Self {
28        OtxRpcError(Box::new(err))
29    }
30}
31
32impl From<VerificationError> for OtxRpcError {
33    fn from(err: VerificationError) -> Self {
34        OtxRpcError(Box::new(err))
35    }
36}
37
38#[derive(Serialize, Deserialize, Clone, Debug, Display, Hash, PartialEq, Eq)]
39pub enum OtxPoolError {
40    #[display(fmt = "Otx already exists")]
41    OtxAlreadyExists,
42}
43
44impl OtxError for OtxPoolError {
45    fn err_code(&self) -> i64 {
46        match self {
47            OtxPoolError::OtxAlreadyExists => -13100,
48        }
49    }
50
51    fn message(&self) -> String {
52        self.to_string()
53    }
54}