Skip to content

feat: add more response helpers #2315

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

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions crates/json-rpc/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ impl<Payload, ErrData> ResponsePacket<Payload, ErrData> {
}
}

/// Returns the first error code in this packet if it contains any error responses.
pub fn first_error_code(&self) -> Option<i64> {
self.as_error().map(|error| error.code)
}

/// Returns the first error message in this packet if it contains any error responses.
pub fn first_error_message(&self) -> Option<&str> {
self.as_error().map(|error| error.message.as_ref())
}

/// Returns the first error data in this packet if it contains any error responses.
pub fn first_error_data(&self) -> Option<&ErrData> {
self.as_error().and_then(|error| error.data.as_ref())
}

/// Returns a all [`Response`].
pub fn responses(&self) -> &[Response<Payload, ErrData>] {
match self {
Expand Down
10 changes: 10 additions & 0 deletions crates/json-rpc/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ impl<Payload, ErrData> Response<Payload, ErrData> {
}
}

/// Returns the payload of this response
pub const fn payload(&self) -> &ResponsePayload<Payload, ErrData> {
&self.payload
}

/// Create a new error response for an internal error with additional data.
pub const fn internal_error_with_obj(id: Id, data: ErrData) -> Self
where
Expand Down Expand Up @@ -117,6 +122,11 @@ impl<Payload, ErrData> Response<Payload, ErrData> {
pub const fn is_error(&self) -> bool {
self.payload.is_error()
}

/// Returns the error code if the payload of this response is an [`ErrorPayload`].
pub fn error_code(&self) -> Option<i64> {
self.payload().error_code()
}
}

impl<Payload, ErrData> Response<Payload, ErrData>
Expand Down
10 changes: 10 additions & 0 deletions crates/json-rpc/src/response/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ impl<Payload, ErrData> ResponsePayload<Payload, ErrData> {
}
}

/// Returns the error code if this a [`ResponsePayload::Failure`]
pub fn error_code(&self) -> Option<i64> {
self.as_error().map(|err| err.code)
}

/// Returns the error data if this a [`ResponsePayload::Failure`]
pub fn error_data(&self) -> Option<&ErrData> {
self.as_error().and_then(|err| err.data.as_ref())
}

/// Returns `true` if the response payload is a success.
pub const fn is_success(&self) -> bool {
matches!(self, Self::Success(_))
Expand Down
Loading