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

fix: map deserde error to ErrorResp if it is an error payload #236

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Changes from all commits
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
24 changes: 17 additions & 7 deletions crates/json-rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ where
pub const fn err_resp(err: ErrorPayload<ErrResp>) -> Self {
Self::ErrorResp(err)
}

/// Instantiate a new `TransportError` from a [`serde_json::Error`] and the
/// text. This should be called when the error occurs during
/// deserialization.
///
/// Note: This will check if the response is actually an [ErrorPayload], if so it will return a
/// [RpcError::ErrorResp].
pub fn deser_err(err: serde_json::Error, text: impl AsRef<str>) -> Self {
let text = text.as_ref();

// check if the response is actually an `ErrorPayload`
if let Ok(err) = serde_json::from_str::<ErrorPayload<ErrResp>>(text) {
return Self::ErrorResp(err);
}

Self::DeserError { err, text: text.to_owned() }
}
}

impl<E, ErrResp> RpcError<E, ErrResp> {
Expand All @@ -57,13 +74,6 @@ impl<E, ErrResp> RpcError<E, ErrResp> {
Self::SerError(err)
}

/// Instantiate a new `TransportError` from a [`serde_json::Error`] and the
/// text. This should be called when the error occurs during
/// deserialization.
pub fn deser_err(err: serde_json::Error, text: impl AsRef<str>) -> Self {
Self::DeserError { err, text: text.as_ref().to_owned() }
}

/// Check if the error is a serialization error.
pub const fn is_ser_error(&self) -> bool {
matches!(self, Self::SerError(_))
Expand Down
Loading