Skip to content

Commit

Permalink
fix: clean up & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heilhead committed Feb 16, 2024
1 parent 803b7ca commit d2a1b93
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 64 deletions.
40 changes: 18 additions & 22 deletions relay_client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,28 @@ pub enum ClientError {

impl From<rpc::ErrorData> for ClientError {
fn from(err: rpc::ErrorData) -> Self {
let rpc::ErrorData {
code,
message,
data,
} = err;

Self::Rpc {
code,
message,
data,
code: err.code,
message: err.message,
data: err.data,
}
}
}

impl ClientError {
pub fn into_service_error<T: ServiceError>(self) -> Error<T> {
match self {
#[derive(Debug, thiserror::Error)]
pub enum Error<T: ServiceError> {
/// Client errors encountered while performing the request.
#[error(transparent)]
Client(ClientError),

/// Error response received from the relay.
#[error(transparent)]
Response(#[from] rpc::Error<T>),
}

impl<T: ServiceError> From<ClientError> for Error<T> {
fn from(err: ClientError) -> Self {
match err {
ClientError::Rpc {
code,
message,
Expand All @@ -101,16 +106,7 @@ impl ClientError {
}
}

_ => Error::Client(self),
_ => Error::Client(err),
}
}
}

#[derive(Debug, thiserror::Error)]
pub enum Error<T: ServiceError> {
#[error(transparent)]
Client(#[from] ClientError),

#[error(transparent)]
Response(#[from] rpc::Error<T>),
}
11 changes: 7 additions & 4 deletions relay_client/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl Client {
let payload = rpc::WatchRegister {
register_auth: claims
.encode(keypair)
.map_err(|err| HttpClientError::Jwt(err).into())
.map_err(HttpClientError::Jwt)
.map_err(ClientError::from)
.map_err(Error::Client)?,
};

Expand Down Expand Up @@ -236,7 +237,8 @@ impl Client {
let payload = rpc::WatchUnregister {
unregister_auth: claims
.encode(keypair)
.map_err(|err| HttpClientError::Jwt(err).into())
.map_err(HttpClientError::Jwt)
.map_err(ClientError::from)
.map_err(Error::Client)?,
};

Expand Down Expand Up @@ -336,7 +338,8 @@ impl Client {
.map_err(|_| HttpClientError::InvalidResponse)
}
.await
.map_err(|err| Error::Client(err.into()))?;
.map_err(ClientError::from)
.map_err(Error::Client)?;

match response {
rpc::Payload::Response(rpc::Response::Success(response)) => {
Expand All @@ -345,7 +348,7 @@ impl Client {
}

rpc::Payload::Response(rpc::Response::Error(response)) => {
Err(ClientError::from(response.error).into_service_error())
Err(ClientError::from(response.error).into())
}

_ => Err(Error::Client(HttpClientError::InvalidResponse.into())),
Expand Down
2 changes: 1 addition & 1 deletion relay_client/src/websocket/outbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where
Err(err) => Err(err),
};

Poll::Ready(result.map_err(ClientError::into_service_error))
Poll::Ready(result.map_err(Into::into))
}
}

Expand Down
30 changes: 15 additions & 15 deletions relay_rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl SuccessfulResponse {
/// Validates the parameters.
pub fn validate(&self) -> Result<(), PayloadError> {
if self.jsonrpc.as_ref() != JSON_RPC_VERSION_STR {
Err(PayloadError::JsonRpcVersion)
Err(PayloadError::InvalidJsonRpcVersion)
} else {
// We can't really validate `serde_json::Value` without knowing the expected
// value type.
Expand Down Expand Up @@ -188,7 +188,7 @@ impl ErrorResponse {
/// Validates the parameters.
pub fn validate(&self) -> Result<(), PayloadError> {
if self.jsonrpc.as_ref() != JSON_RPC_VERSION_STR {
Err(PayloadError::JsonRpcVersion)
Err(PayloadError::InvalidJsonRpcVersion)
} else {
Ok(())
}
Expand Down Expand Up @@ -234,7 +234,7 @@ impl ServiceRequest for Subscribe {
fn validate(&self) -> Result<(), PayloadError> {
self.topic
.decode()
.map_err(|_| PayloadError::TopicDecoding)?;
.map_err(|_| PayloadError::InvalidTopic)?;

Ok(())
}
Expand Down Expand Up @@ -262,7 +262,7 @@ impl ServiceRequest for Unsubscribe {
fn validate(&self) -> Result<(), PayloadError> {
self.topic
.decode()
.map_err(|_| PayloadError::TopicDecoding)?;
.map_err(|_| PayloadError::InvalidTopic)?;

// FIXME: Subscription ID validation is currently disabled, since SDKs do not
// use the actual IDs generated by the relay, and instead send some randomized
Expand Down Expand Up @@ -295,7 +295,7 @@ impl ServiceRequest for FetchMessages {
fn validate(&self) -> Result<(), PayloadError> {
self.topic
.decode()
.map_err(|_| PayloadError::TopicDecoding)?;
.map_err(|_| PayloadError::InvalidTopic)?;

Ok(())
}
Expand Down Expand Up @@ -345,7 +345,7 @@ impl ServiceRequest for BatchSubscribe {
}

for topic in &self.topics {
topic.decode().map_err(|_| PayloadError::TopicDecoding)?;
topic.decode().map_err(|_| PayloadError::InvalidTopic)?;
}

Ok(())
Expand Down Expand Up @@ -413,7 +413,7 @@ impl ServiceRequest for BatchFetchMessages {
}

for topic in &self.topics {
topic.decode().map_err(|_| PayloadError::TopicDecoding)?;
topic.decode().map_err(|_| PayloadError::InvalidTopic)?;
}

Ok(())
Expand Down Expand Up @@ -460,7 +460,7 @@ impl ServiceRequest for BatchReceiveMessages {
receipt
.topic
.decode()
.map_err(|_| PayloadError::TopicDecoding)?;
.map_err(|_| PayloadError::InvalidTopic)?;
}

Ok(())
Expand Down Expand Up @@ -544,7 +544,7 @@ impl ServiceRequest for Publish {
fn validate(&self) -> Result<(), PayloadError> {
self.topic
.decode()
.map_err(|_| PayloadError::TopicDecoding)?;
.map_err(|_| PayloadError::InvalidTopic)?;

Ok(())
}
Expand Down Expand Up @@ -581,8 +581,8 @@ pub enum WatchError {
#[error("Invalid action")]
InvalidAction,

#[error("Failed to decode JWT")]
Jwt,
#[error("Invalid JWT")]
InvalidJwt,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -651,12 +651,12 @@ impl ServiceRequest for Subscription {
fn validate(&self) -> Result<(), PayloadError> {
self.id
.decode()
.map_err(|_| PayloadError::SubscriptionIdDecoding)?;
.map_err(|_| PayloadError::InvalidSubscriptionId)?;

self.data
.topic
.decode()
.map_err(|_| PayloadError::TopicDecoding)?;
.map_err(|_| PayloadError::InvalidTopic)?;

Ok(())
}
Expand Down Expand Up @@ -767,11 +767,11 @@ impl Request {
/// Validates the request payload.
pub fn validate(&self) -> Result<(), PayloadError> {
if !self.id.validate() {
return Err(PayloadError::RequestId);
return Err(PayloadError::InvalidRequestId);
}

if self.jsonrpc.as_ref() != JSON_RPC_VERSION_STR {
return Err(PayloadError::JsonRpcVersion);
return Err(PayloadError::InvalidJsonRpcVersion);
}

match &self.params {
Expand Down
14 changes: 7 additions & 7 deletions relay_rpc/src/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub enum AuthError {
OriginNotAllowed,

#[error("Invalid JWT")]
JwtInvalid,
InvalidJwt,

#[error("Missing JWT")]
JwtMissing,
MissingJwt,

#[error("Country blocked")]
CountryBlocked,
Expand All @@ -65,16 +65,16 @@ pub enum PayloadError {
PayloadSizeExceeded,

#[error("Topic decoding failed")]
TopicDecoding,
InvalidTopic,

#[error("Subscription ID decoding failed")]
SubscriptionIdDecoding,
InvalidSubscriptionId,

#[error("Invalid request ID")]
RequestId,
InvalidRequestId,

#[error("Invalid JSON RPC version")]
JsonRpcVersion,
InvalidJsonRpcVersion,

#[error("The batch contains too many items")]
BatchLimitExceeded,
Expand All @@ -95,7 +95,7 @@ pub enum InternalError {
Serialization,

#[error("Internal error")]
Other,
Unknown,
}

/// Errors caught while processing the request. These are meant to be serialized
Expand Down
Loading

0 comments on commit d2a1b93

Please sign in to comment.