Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: RpcError with custom client error #694

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion subxt/src/client/online_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<T: Config> OnlineClient<T> {
pub async fn from_url(url: impl AsRef<str>) -> Result<OnlineClient<T>, Error> {
let client = jsonrpsee_helpers::ws_client(url.as_ref())
.await
.map_err(|e| crate::error::RpcError(e.to_string()))?;
.map_err(|e| crate::error::RpcError::ClientError(Box::new(e)))?;
OnlineClient::from_rpc_client(Arc::new(client)).await
}
}
Expand Down
11 changes: 9 additions & 2 deletions subxt/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ impl From<DispatchError> for Error {
}

/// An RPC error. Since we are generic over the RPC client that is used,
/// the error is any custom string.
/// the error is boxed and could be casted.
#[derive(Debug, thiserror::Error)]
#[error("RPC error: {0}")]
pub struct RpcError(pub String);
pub enum RpcError {
// Dev note: We need the error to be safely sent between threads
// for `subscribe_to_block_headers_filling_in_gaps` and friends.
/// Error related to the RPC client.
ClientError(Box<dyn std::error::Error + Send + 'static>),
/// Custom error.
Custom(String),
}
lexnv marked this conversation as resolved.
Show resolved Hide resolved

/// This is our attempt to decode a runtime DispatchError. We either
/// successfully decode it into a [`ModuleError`], or we fail and keep
Expand Down
8 changes: 4 additions & 4 deletions subxt/src/rpc/jsonrpsee_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl RpcClientT for Client {
let params = prep_params_for_jsonrpsee(params)?;
let res = ClientT::request(self, method, Some(params))
.await
.map_err(|e| RpcError(e.to_string()))?;
.map_err(|e| RpcError::ClientError(Box::new(e)))?;
Ok(res)
})
}
Expand All @@ -55,8 +55,8 @@ impl RpcClientT for Client {
unsub,
)
.await
.map_err(|e| RpcError(e.to_string()))?
.map_err(|e| RpcError(e.to_string()))
.map_err(|e| RpcError::ClientError(Box::new(e)))?
.map_err(|e| RpcError::ClientError(Box::new(e)))
.boxed();
Ok(sub)
})
Expand All @@ -77,7 +77,7 @@ fn prep_params_for_jsonrpsee(
let arr = match val {
Value::Array(arr) => Ok(arr),
_ => {
Err(RpcError(format!(
Err(RpcError::Custom(format!(
"RPC Params are expected to be an array but got {params}"
)))
}
Expand Down
4 changes: 2 additions & 2 deletions subxt/src/tx/tx_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ where
_ => continue,
}
}
Err(RpcError("RPC subscription dropped".to_string()).into())
Err(RpcError::Custom("RPC subscription dropped".into()).into())
}

/// Wait for the transaction to be finalized, and return a [`TxInBlock`]
Expand All @@ -133,7 +133,7 @@ where
_ => continue,
}
}
Err(RpcError("RPC subscription dropped".to_string()).into())
Err(RpcError::Custom("RPC subscription dropped".into()).into())
}

/// Wait for the transaction to be finalized, and for the transaction events to indicate
Expand Down