jsonrpc_quic/
errors.rs

1// Copyright 2020 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10use std::fmt;
11
12#[derive(Debug)]
13pub enum Error {
14    ClientError(String),
15    RemoteEndpointError(String),
16    GeneralError(String),
17}
18
19impl From<std::io::Error> for Error {
20    fn from(error: std::io::Error) -> Self {
21        Error::GeneralError(error.to_string())
22    }
23}
24
25pub type Result<T> = std::result::Result<T, Error>;
26
27impl From<Error> for String {
28    fn from(error: Error) -> String {
29        error.to_string()
30    }
31}
32
33impl fmt::Display for Error {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        use Error::*;
36
37        let (error_type, error_msg) = match self {
38            ClientError(info) => ("ClientError", info),
39            RemoteEndpointError(info) => ("RemoteEndpointError", info),
40            GeneralError(info) => ("GeneralError", info),
41        };
42        let description = format!("[Error] {} - {}", error_type, error_msg);
43
44        write!(f, "{}", description)
45    }
46}