substrate_api_client/rpc/
error.rs

1/*
2   Copyright 2019 Supercomputing Systems AG
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8	   http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15
16*/
17
18use alloc::{boxed::Box, string::String};
19use core::error::Error as ErrorT;
20
21pub type Result<T> = core::result::Result<T, Error>;
22
23#[derive(Debug)]
24pub enum Error {
25	SerdeJson(serde_json::error::Error),
26	ExtrinsicFailed(String),
27	MpscSend(String),
28	InvalidUrl(String),
29	RecvError(String),
30	Io(String),
31	MaxConnectionAttemptsExceeded,
32	ConnectionClosed,
33	Client(Box<dyn ErrorT + Send + Sync + 'static>),
34}
35
36impl From<serde_json::error::Error> for Error {
37	fn from(error: serde_json::error::Error) -> Self {
38		Self::SerdeJson(error)
39	}
40}
41
42#[cfg(feature = "ws-client")]
43impl From<ws::Error> for Error {
44	fn from(error: ws::Error) -> Self {
45		Self::Client(Box::new(error))
46	}
47}
48
49#[cfg(feature = "tungstenite-client")]
50impl From<tungstenite::Error> for Error {
51	fn from(error: tungstenite::Error) -> Self {
52		Self::Client(Box::new(error))
53	}
54}
55
56#[cfg(feature = "std")]
57#[allow(unused_imports)]
58pub use std_only::*;
59#[cfg(feature = "std")]
60mod std_only {
61	use super::*;
62	use std::sync::mpsc::{RecvError, SendError};
63
64	impl From<SendError<String>> for Error {
65		fn from(error: SendError<String>) -> Self {
66			Self::MpscSend(error.0)
67		}
68	}
69
70	impl From<RecvError> for Error {
71		fn from(error: RecvError) -> Self {
72			Self::RecvError(format!("{error:?}"))
73		}
74	}
75
76	impl From<std::io::Error> for Error {
77		fn from(error: std::io::Error) -> Self {
78			Self::Io(format!("{error:?}"))
79		}
80	}
81
82	impl From<url::ParseError> for Error {
83		fn from(error: url::ParseError) -> Self {
84			Self::InvalidUrl(format!("{error:?}"))
85		}
86	}
87}