-
Notifications
You must be signed in to change notification settings - Fork 246
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
refactor: RpcError and RpcResult and TransportError and TransportResult #28
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use serde_json::value::RawValue; | ||
|
||
use crate::{ErrorPayload, RpcReturn}; | ||
|
||
/// An RPC error. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum RpcError<E, ErrResp = Box<RawValue>> { | ||
/// Server returned an error response. | ||
#[error("Server returned an error response: {0}")] | ||
ErrorResp(ErrorPayload<ErrResp>), | ||
|
||
/// JSON serialization error. | ||
#[error("Serialization error: {0}")] | ||
SerError( | ||
/// The underlying serde_json error. | ||
// To avoid accidentally confusing ser and deser errors, we do not use | ||
// the `#[from]` tag. | ||
#[source] | ||
serde_json::Error, | ||
), | ||
/// JSON deserialization error. | ||
#[error("Deserialization error: {err}")] | ||
DeserError { | ||
/// The underlying serde_json error. | ||
// To avoid accidentally confusing ser and deser errors, we do not use | ||
// the `#[from]` tag. | ||
#[source] | ||
err: serde_json::Error, | ||
/// For deser errors, the text that failed to deserialize. | ||
text: String, | ||
}, | ||
|
||
/// Transport error. | ||
/// | ||
/// This variant is used when the error occurs during communication. | ||
#[error("Error during transport: {0}")] | ||
Transport( | ||
/// The underlying transport error. | ||
#[from] | ||
E, | ||
), | ||
} | ||
|
||
impl<E, ErrResp> RpcError<E, ErrResp> | ||
where | ||
ErrResp: RpcReturn, | ||
{ | ||
/// Instantiate a new `TransportError` from an error response. | ||
pub const fn err_resp(err: ErrorPayload<ErrResp>) -> Self { | ||
Self::ErrorResp(err) | ||
} | ||
} | ||
|
||
impl<E, ErrResp> RpcError<E, ErrResp> { | ||
/// Instantiate a new `TransportError` from a [`serde_json::Error`]. This | ||
/// should be called when the error occurs during serialization. | ||
pub const fn ser_err(err: serde_json::Error) -> Self { | ||
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(_)) | ||
} | ||
|
||
/// Check if the error is a deserialization error. | ||
pub const fn is_deser_error(&self) -> bool { | ||
matches!(self, Self::DeserError { .. }) | ||
} | ||
|
||
/// Check if the error is a transport error. | ||
pub const fn is_transport_error(&self) -> bool { | ||
matches!(self, Self::Transport(_)) | ||
} | ||
|
||
/// Check if the error is an error response. | ||
pub const fn is_error_resp(&self) -> bool { | ||
matches!(self, Self::ErrorResp(_)) | ||
} | ||
|
||
/// Fallible conversion to an error response. | ||
pub const fn as_error_resp(&self) -> Option<&ErrorPayload<ErrResp>> { | ||
match self { | ||
Self::ErrorResp(err) => Some(err), | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.