make request error to be confined per request, not a global websocket error (#315)

This commit is contained in:
guanqun 2021-06-12 15:46:19 +08:00 committed by GitHub
parent f3cddc19f8
commit 34d38ca40f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -43,7 +43,7 @@ pub struct Ws {
requests: mpsc::UnboundedSender<TransportMessage>, requests: mpsc::UnboundedSender<TransportMessage>,
} }
type Pending = oneshot::Sender<serde_json::Value>; type Pending = oneshot::Sender<Result<serde_json::Value, JsonRpcError>>;
type Subscription = mpsc::UnboundedSender<serde_json::Value>; type Subscription = mpsc::UnboundedSender<serde_json::Value>;
enum TransportMessage { enum TransportMessage {
@ -129,6 +129,9 @@ impl JsonRpcClient for Ws {
// wait for the response // wait for the response
let res = receiver.await?; let res = receiver.await?;
// in case the request itself has any errors
let res = res?;
// parse it // parse it
Ok(serde_json::from_value(res)?) Ok(serde_json::from_value(res)?)
} }
@ -268,7 +271,7 @@ where
if let Ok(resp) = serde_json::from_str::<Response<serde_json::Value>>(&inner) { if let Ok(resp) = serde_json::from_str::<Response<serde_json::Value>>(&inner) {
if let Some(request) = self.pending.remove(&resp.id) { if let Some(request) = self.pending.remove(&resp.id) {
request request
.send(resp.data.into_result()?) .send(resp.data.into_result())
.map_err(to_client_error)?; .map_err(to_client_error)?;
} }
} else if let Ok(notification) = } else if let Ok(notification) =
@ -286,8 +289,8 @@ where
} }
// TrySendError is private :( // TrySendError is private :(
fn to_client_error<T: ToString>(err: T) -> ClientError { fn to_client_error<T: Debug>(err: T) -> ClientError {
ClientError::ChannelError(err.to_string()) ClientError::ChannelError(format!("{:?}", err))
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]