xpx_chain_sdk/api/
error.rs

1/*
2 * Copyright 2018 ProximaX Limited. All rights reserved.
3 * Use of this source code is governed by the Apache 2.0
4 * license that can be found in the LICENSE file.
5 */
6
7use std::result;
8
9use tower::BoxError;
10
11use crate::api;
12
13/// Result type of all Websocket library calls.
14pub type Result<T> = result::Result<T, Error>;
15
16#[derive(Debug, Error, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct SiriusError {
19    pub code: String,
20    pub message: String,
21}
22
23#[derive(Debug, Error)]
24pub enum Error {
25    #[error("{0}")]
26    Serde(#[from] serde_json::Error),
27    #[error("{0}")]
28    SiriusError(#[from] SiriusError),
29    #[error("{0}")]
30    Hyper(#[from] hyper::Error),
31    #[error("{0}")]
32    Transport(#[from] api::transport::Error),
33    #[error("{0}")]
34    BuildUninitializedField(#[from] derive_builder::UninitializedFieldError),
35    #[error(transparent)]
36    Other(#[from] anyhow::Error),
37    #[error("{0}")]
38    Tower(#[from] BoxError),
39}
40
41impl From<&'static str> for Error {
42    fn from(msg: &'static str) -> Self {
43        Error::Other(anyhow!(msg))
44    }
45}
46
47impl core::fmt::Display for SiriusError {
48    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
49        write!(f, "{{ code: \"{}\", message: \"{}\" }}", self.code, self.message)
50    }
51}