1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use anyhow::anyhow;
use plutus_ledger_api::csl::{csl_to_pla::TryFromCSLError, pla_to_csl::TryFromPLAError};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
use tx_bakery::{chain_query::ChainQueryError, submitter::SubmitterError};

pub type Result<T> = std::result::Result<T, OgmiosError>;

#[derive(Error, Debug)]
pub enum OgmiosError {
    #[error(transparent)]
    TryFromPLAError(#[from] TryFromPLAError),

    #[error(transparent)]
    TryFromCSLError(#[from] TryFromCSLError),

    #[error("Couldn't convert a {label} from Ogmios response: {source}")]
    ConversionError {
        label: String,
        source: anyhow::Error,
    },

    #[error(transparent)]
    RequestError(#[from] reqwest::Error),

    #[error(transparent)]
    IOError(#[from] std::io::Error),

    #[error("Failed to start Ogmios: {0}")]
    StartupError(anyhow::Error),

    #[error(transparent)]
    JSONRpcError(#[from] jsonrpsee::core::client::Error),
}

impl From<OgmiosError> for ChainQueryError {
    fn from(err: OgmiosError) -> ChainQueryError {
        ChainQueryError(anyhow!(err))
    }
}

impl From<OgmiosError> for SubmitterError {
    fn from(err: OgmiosError) -> SubmitterError {
        SubmitterError(anyhow!(err))
    }
}

#[derive(Error, Clone, Debug, Serialize, Deserialize)]
#[error("JsonRPCError {code}: {message} ({data:?})")]
pub struct JsonRPCError {
    pub code: i16,
    pub message: String,
    pub data: Option<Value>,
}