jsonrpc_sdk_prelude/
error.rs

1// Copyright (C) 2019 Boyu Yang
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use jsonrpc_core::error::Error as CoreError;
10use reqwest::Error as ReqwestError;
11
12#[derive(Debug)]
13pub enum Error {
14    OptionNone,
15    Custom(String),
16    Core(CoreError),
17    Serde(String),
18    Reqwest(ReqwestError),
19}
20
21impl ::std::fmt::Display for Error {
22    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
23        write!(f, "{:?}", self)
24    }
25}
26
27impl ::std::error::Error for Error {}
28
29pub type Result<T> = ::std::result::Result<T, Error>;
30
31impl Error {
32    pub fn none() -> Self {
33        Error::OptionNone
34    }
35
36    pub fn custom(msg: &str) -> Self {
37        Error::Custom(msg.to_owned())
38    }
39
40    pub fn serde(msg: &str) -> Self {
41        Error::Serde(msg.to_owned())
42    }
43}
44
45impl From<CoreError> for Error {
46    fn from(err: CoreError) -> Self {
47        Error::Core(err)
48    }
49}
50
51impl From<ReqwestError> for Error {
52    fn from(err: ReqwestError) -> Self {
53        Error::Reqwest(err)
54    }
55}