From 803b7caf44ca83fc6f7c69a2f88315ae204e0a3a Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Thu, 15 Feb 2024 19:03:33 +0100 Subject: [PATCH] fix: update http client --- relay_client/src/error.rs | 20 +++++++- relay_client/src/http.rs | 71 ++++++++++++++++----------- relay_client/src/websocket/inbound.rs | 2 +- relay_client/src/websocket/stream.rs | 18 +++---- relay_rpc/src/rpc/error.rs | 12 ++--- 5 files changed, 72 insertions(+), 51 deletions(-) diff --git a/relay_client/src/error.rs b/relay_client/src/error.rs index 9d81059..28262e6 100644 --- a/relay_client/src/error.rs +++ b/relay_client/src/error.rs @@ -64,6 +64,22 @@ pub enum ClientError { InvalidRequestType, } +impl From for ClientError { + fn from(err: rpc::ErrorData) -> Self { + let rpc::ErrorData { + code, + message, + data, + } = err; + + Self::Rpc { + code, + message, + data, + } + } +} + impl ClientError { pub fn into_service_error(self) -> Error { match self { @@ -79,7 +95,7 @@ impl ClientError { }; match rpc::Error::try_from(err) { - Ok(err) => Error::Request(err), + Ok(err) => Error::Response(err), Err(_) => Error::Client(ClientError::InvalidErrorResponse), } @@ -96,5 +112,5 @@ pub enum Error { Client(#[from] ClientError), #[error(transparent)] - Request(#[from] rpc::Error), + Response(#[from] rpc::Error), } diff --git a/relay_client/src/http.rs b/relay_client/src/http.rs index 4fa6157..3839eb9 100644 --- a/relay_client/src/http.rs +++ b/relay_client/src/http.rs @@ -1,6 +1,6 @@ use { crate::{ - error::{BoxError, ClientError}, + error::{BoxError, ClientError, Error}, ConnectionOptions, MessageIdGenerator, }, @@ -16,8 +16,8 @@ use { }; pub type TransportError = reqwest::Error; -pub type Response = Result<::Response, ClientError>; -pub type EmptyResponse = Result<(), ClientError>; +pub type Response = Result<::Response, Error<::Error>>; +pub type EmptyResponse = Result<(), Error<::Error>>; #[derive(Debug, thiserror::Error)] pub enum RequestParamsError { @@ -41,9 +41,6 @@ pub enum HttpClientError { #[error("JWT error: {0}")] Jwt(#[from] JwtError), - - #[error("RPC error: code={} message={}", .0.code, .0.message)] - RpcError(rpc::ErrorData), } #[derive(Debug, Clone)] @@ -111,11 +108,14 @@ impl Client { tag: u32, ttl: Duration, prompt: bool, - ) -> EmptyResponse { + ) -> EmptyResponse { let ttl_secs = ttl .as_secs() .try_into() - .map_err(|_| HttpClientError::InvalidRequest(RequestParamsError::InvalidTtl.into()))?; + .map_err(|_| { + HttpClientError::InvalidRequest(RequestParamsError::InvalidTtl.into()).into() + }) + .map_err(Error::Client)?; self.request(rpc::Publish { topic, @@ -175,7 +175,8 @@ impl Client { .ttl .as_secs() .try_into() - .map_err(|err| HttpClientError::InvalidRequest(Box::new(err)))?; + .map_err(|err| HttpClientError::InvalidRequest(Box::new(err)).into()) + .map_err(Error::Client)?; let exp = iat + ttl_sec; let claims = rpc::WatchRegisterClaims { @@ -194,7 +195,10 @@ impl Client { }; let payload = rpc::WatchRegister { - register_auth: claims.encode(keypair).map_err(HttpClientError::Jwt)?, + register_auth: claims + .encode(keypair) + .map_err(|err| HttpClientError::Jwt(err).into()) + .map_err(Error::Client)?, }; self.request(payload).await @@ -230,7 +234,10 @@ impl Client { }; let payload = rpc::WatchUnregister { - unregister_auth: claims.encode(keypair).map_err(HttpClientError::Jwt)?, + unregister_auth: claims + .encode(keypair) + .map_err(|err| HttpClientError::Jwt(err).into()) + .map_err(Error::Client)?, }; self.request(payload).await @@ -307,37 +314,41 @@ impl Client { params: payload.into_params(), }); - let result = self - .client - .post(self.url.clone()) - .json(&payload) - .send() - .await - .map_err(HttpClientError::Transport)?; + let response = async { + let result = self + .client + .post(self.url.clone()) + .json(&payload) + .send() + .await + .map_err(HttpClientError::Transport)?; - let status = result.status(); + let status = result.status(); - if !status.is_success() { - let body = result.text().await; - return Err(HttpClientError::InvalidHttpCode(status, body).into()); - } + if !status.is_success() { + let body = result.text().await; + return Err(HttpClientError::InvalidHttpCode(status, body)); + } - let response = result - .json::() - .await - .map_err(|_| HttpClientError::InvalidResponse)?; + result + .json::() + .await + .map_err(|_| HttpClientError::InvalidResponse) + } + .await + .map_err(|err| Error::Client(err.into()))?; match response { rpc::Payload::Response(rpc::Response::Success(response)) => { serde_json::from_value(response.result) - .map_err(|_| HttpClientError::InvalidResponse.into()) + .map_err(|_| Error::Client(HttpClientError::InvalidResponse.into())) } rpc::Payload::Response(rpc::Response::Error(response)) => { - Err(HttpClientError::RpcError(response.error).into()) + Err(ClientError::from(response.error).into_service_error()) } - _ => Err(HttpClientError::InvalidResponse.into()), + _ => Err(Error::Client(HttpClientError::InvalidResponse.into())), } } } diff --git a/relay_client/src/websocket/inbound.rs b/relay_client/src/websocket/inbound.rs index 85622bd..2581c7d 100644 --- a/relay_client/src/websocket/inbound.rs +++ b/relay_client/src/websocket/inbound.rs @@ -52,7 +52,7 @@ where serde_json::to_value(data).map_err(ClientError::Serialization)?, )), - Err(err) => Response::Error(ErrorResponse::new(self.id, rpc::Error::Request(err))), + Err(err) => Response::Error(ErrorResponse::new(self.id, rpc::Error::Handler(err))), }; let message = Message::Text( diff --git a/relay_client/src/websocket/stream.rs b/relay_client/src/websocket/stream.rs index 362a87b..28a5e1e 100644 --- a/relay_client/src/websocket/stream.rs +++ b/relay_client/src/websocket/stream.rs @@ -185,13 +185,9 @@ impl ClientStream { if id.is_zero() { return match response { - Response::Error(response) => { - Some(StreamEvent::InboundError(ClientError::Rpc { - code: response.error.code, - message: response.error.message, - data: response.error.data, - })) - } + Response::Error(response) => Some(StreamEvent::InboundError( + ClientError::from(response.error), + )), Response::Success(_) => Some(StreamEvent::InboundError( ClientError::InvalidResponseId, @@ -201,11 +197,9 @@ impl ClientStream { if let Some(tx) = self.requests.remove(&id) { let result = match response { - Response::Error(response) => Err(ClientError::Rpc { - code: response.error.code, - message: response.error.message, - data: response.error.data, - }), + Response::Error(response) => { + Err(ClientError::from(response.error)) + } Response::Success(response) => Ok(response.result), }; diff --git a/relay_rpc/src/rpc/error.rs b/relay_rpc/src/rpc/error.rs index f31e57a..7aa9db4 100644 --- a/relay_rpc/src/rpc/error.rs +++ b/relay_rpc/src/rpc/error.rs @@ -109,8 +109,8 @@ pub enum Error { #[error("Invalid payload: {0}")] Payload(#[from] PayloadError), - #[error("Request error: {0}")] - Request(T), + #[error("Request handler error: {0}")] + Handler(T), #[error("Internal error: {0}")] Internal(#[from] InternalError), @@ -125,7 +125,7 @@ impl Error { Self::Auth(_) => ERROR_CODE_AUTH, Self::TooManyRequests => ERROR_CODE_TOO_MANY_REQUESTS, Self::Payload(_) => ERROR_CODE_PAYLOAD, - Self::Request(_) => ERROR_CODE_REQUEST, + Self::Handler(_) => ERROR_CODE_HANDLER, Self::Internal(_) => ERROR_CODE_INTERNAL, } } @@ -134,7 +134,7 @@ impl Error { match &self { Self::Auth(err) => err.tag(), Self::Payload(err) => err.tag(), - Self::Request(err) => err.tag(), + Self::Handler(err) => err.tag(), Self::Internal(err) => err.tag(), Self::TooManyRequests => self.tag(), } @@ -144,7 +144,7 @@ impl Error { pub const ERROR_CODE_AUTH: i32 = 3000; pub const ERROR_CODE_TOO_MANY_REQUESTS: i32 = 3001; pub const ERROR_CODE_PAYLOAD: i32 = -32600; -pub const ERROR_CODE_REQUEST: i32 = -32000; +pub const ERROR_CODE_HANDLER: i32 = -32000; pub const ERROR_CODE_INTERNAL: i32 = -32603; #[derive(Debug, thiserror::Error)] @@ -161,7 +161,7 @@ impl TryFrom for Error { ERROR_CODE_AUTH => Error::Auth(try_parse_error(tag)?), ERROR_CODE_TOO_MANY_REQUESTS => Error::TooManyRequests, ERROR_CODE_PAYLOAD => Error::Payload(try_parse_error(tag)?), - ERROR_CODE_REQUEST => Error::Request(try_parse_error(tag)?), + ERROR_CODE_HANDLER => Error::Handler(try_parse_error(tag)?), ERROR_CODE_INTERNAL => Error::Internal(try_parse_error(tag)?), _ => return Err(InvalidErrorData), };