2019-04-25 11:29:44 -06:00
|
|
|
use crate::rpc_request;
|
2020-03-13 00:20:49 -06:00
|
|
|
use solana_sdk::{
|
|
|
|
signature::SignerError, transaction::TransactionError, transport::TransportError,
|
|
|
|
};
|
|
|
|
use std::io;
|
2020-02-20 20:04:53 -07:00
|
|
|
use thiserror::Error;
|
2019-04-25 11:29:44 -06:00
|
|
|
|
2020-02-20 20:04:53 -07:00
|
|
|
#[derive(Error, Debug)]
|
2020-03-13 00:20:49 -06:00
|
|
|
pub enum ClientErrorKind {
|
|
|
|
#[error(transparent)]
|
2020-02-20 20:04:53 -07:00
|
|
|
Io(#[from] io::Error),
|
2020-03-13 00:20:49 -06:00
|
|
|
#[error(transparent)]
|
2020-02-20 20:04:53 -07:00
|
|
|
Reqwest(#[from] reqwest::Error),
|
2020-03-13 00:20:49 -06:00
|
|
|
#[error(transparent)]
|
2020-02-20 20:04:53 -07:00
|
|
|
RpcError(#[from] rpc_request::RpcError),
|
2020-03-13 00:20:49 -06:00
|
|
|
#[error(transparent)]
|
2020-02-20 20:04:53 -07:00
|
|
|
SerdeJson(#[from] serde_json::error::Error),
|
2020-03-13 00:20:49 -06:00
|
|
|
#[error(transparent)]
|
2020-02-20 20:04:53 -07:00
|
|
|
SigningError(#[from] SignerError),
|
2020-03-13 00:20:49 -06:00
|
|
|
#[error(transparent)]
|
2020-02-20 20:04:53 -07:00
|
|
|
TransactionError(#[from] TransactionError),
|
2020-03-13 00:20:49 -06:00
|
|
|
#[error("Custom: {0}")]
|
|
|
|
Custom(String),
|
2019-04-25 11:29:44 -06:00
|
|
|
}
|
|
|
|
|
2020-03-13 00:20:49 -06:00
|
|
|
impl From<TransportError> for ClientErrorKind {
|
|
|
|
fn from(err: TransportError) -> Self {
|
|
|
|
match err {
|
|
|
|
TransportError::IoError(err) => Self::Io(err),
|
|
|
|
TransportError::TransactionError(err) => Self::TransactionError(err),
|
|
|
|
TransportError::Custom(err) => Self::Custom(err),
|
|
|
|
}
|
2019-04-25 11:29:44 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-13 00:20:49 -06:00
|
|
|
|
|
|
|
impl Into<TransportError> for ClientErrorKind {
|
|
|
|
fn into(self) -> TransportError {
|
|
|
|
match self {
|
|
|
|
Self::Io(err) => TransportError::IoError(err),
|
|
|
|
Self::TransactionError(err) => TransportError::TransactionError(err),
|
|
|
|
Self::Reqwest(err) => TransportError::Custom(format!("{:?}", err)),
|
|
|
|
Self::RpcError(err) => TransportError::Custom(format!("{:?}", err)),
|
|
|
|
Self::SerdeJson(err) => TransportError::Custom(format!("{:?}", err)),
|
|
|
|
Self::SigningError(err) => TransportError::Custom(format!("{:?}", err)),
|
|
|
|
Self::Custom(err) => TransportError::Custom(format!("{:?}", err)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
#[error("{kind}")]
|
|
|
|
pub struct ClientError {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: Option<rpc_request::RpcRequest>,
|
|
|
|
|
2020-03-13 00:20:49 -06:00
|
|
|
#[source]
|
|
|
|
kind: ClientErrorKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientError {
|
2020-05-10 08:51:53 -07:00
|
|
|
pub fn new_with_request(kind: ClientErrorKind, request: rpc_request::RpcRequest) -> Self {
|
2020-03-13 00:20:49 -06:00
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: Some(request),
|
2020-03-13 00:20:49 -06:00
|
|
|
kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:51:53 -07:00
|
|
|
pub fn into_with_request(self, request: rpc_request::RpcRequest) -> Self {
|
2020-03-13 00:20:49 -06:00
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: Some(request),
|
2020-03-13 00:20:49 -06:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:51:53 -07:00
|
|
|
pub fn request(&self) -> Option<&rpc_request::RpcRequest> {
|
|
|
|
self.request.as_ref()
|
2020-03-13 00:20:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn kind(&self) -> &ClientErrorKind {
|
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ClientErrorKind> for ClientError {
|
|
|
|
fn from(kind: ClientErrorKind) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TransportError> for ClientError {
|
|
|
|
fn from(err: TransportError) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<TransportError> for ClientError {
|
|
|
|
fn into(self) -> TransportError {
|
|
|
|
self.kind.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for ClientError {
|
|
|
|
fn from(err: std::io::Error) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for ClientError {
|
|
|
|
fn from(err: reqwest::Error) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<rpc_request::RpcError> for ClientError {
|
|
|
|
fn from(err: rpc_request::RpcError) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::error::Error> for ClientError {
|
|
|
|
fn from(err: serde_json::error::Error) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SignerError> for ClientError {
|
|
|
|
fn from(err: SignerError) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TransactionError> for ClientError {
|
|
|
|
fn from(err: TransactionError) -> Self {
|
|
|
|
Self {
|
2020-05-10 08:51:53 -07:00
|
|
|
request: None,
|
2020-03-13 00:20:49 -06:00
|
|
|
kind: err.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, ClientError>;
|