-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(grpc): introduce Status and ServerStatus types #2454
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
Open
sauravzg
wants to merge
2
commits into
hyperium:master
Choose a base branch
from
sauravzg:grpc_status
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 hidden or 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 hidden or 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,53 @@ | ||
| mod server_status; | ||
| mod status_code; | ||
|
|
||
| pub use server_status::ServerStatus; | ||
| pub use status_code::StatusCode; | ||
|
|
||
| /// Represents a gRPC status. | ||
| #[derive(Debug, Clone)] | ||
| pub struct Status { | ||
| code: StatusCode, | ||
| message: String, | ||
| } | ||
|
|
||
| impl Status { | ||
| /// Create a new `Status` with the given code and message. | ||
| pub fn new(code: StatusCode, message: impl Into<String>) -> Self { | ||
| Status { | ||
| code, | ||
| message: message.into(), | ||
| } | ||
| } | ||
|
|
||
| /// Get the `StatusCode` of this `Status`. | ||
| pub fn code(&self) -> StatusCode { | ||
| self.code | ||
| } | ||
|
|
||
| /// Get the message of this `Status`. | ||
| pub fn message(&self) -> &str { | ||
| &self.message | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_status_new() { | ||
| let status = Status::new(StatusCode::Ok, "ok"); | ||
| assert_eq!(status.code(), StatusCode::Ok); | ||
| assert_eq!(status.message(), "ok"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_status_debug() { | ||
| let status = Status::new(StatusCode::Ok, "ok"); | ||
| let debug = format!("{:?}", status); | ||
| assert!(debug.contains("Status")); | ||
| assert!(debug.contains("Ok")); | ||
| assert!(debug.contains("ok")); | ||
| } | ||
| } | ||
This file contains hidden or 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,66 @@ | ||
| use super::status_code::StatusCode; | ||
| use super::Status; | ||
|
|
||
| /// Represents a gRPC status on the server. | ||
| /// | ||
| /// This is a separate type from `Status` to prevent accidental conversion and | ||
| /// leaking of sensitive information from the server to the client. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ServerStatus(Status); | ||
|
|
||
| impl std::ops::Deref for ServerStatus { | ||
| type Target = Status; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl ServerStatus { | ||
| /// Create a new `ServerStatus` with the given code and message. | ||
| pub fn new(code: StatusCode, message: impl Into<String>) -> Self { | ||
| ServerStatus(Status::new(code, message)) | ||
| } | ||
|
|
||
| /// Create a new `ServerStatus` from a `Status`. | ||
| pub fn from_status(status: Status) -> Self { | ||
| ServerStatus(status) | ||
| } | ||
|
|
||
| /// Converts the `ServerStatus` to a `Status` for client responses. | ||
| pub(crate) fn into_status(self) -> Status { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_server_status_new() { | ||
| let status = ServerStatus::new(StatusCode::Ok, "ok"); | ||
| assert_eq!(status.code(), StatusCode::Ok); | ||
| assert_eq!(status.message(), "ok"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_server_status_deref() { | ||
| let status = ServerStatus::new(StatusCode::Ok, "ok"); | ||
| assert_eq!(status.code(), StatusCode::Ok); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_server_status_from_status() { | ||
| let status = Status::new(StatusCode::Ok, "ok"); | ||
| let server_status = ServerStatus::from_status(status); | ||
| assert_eq!(server_status.code(), StatusCode::Ok); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_server_status_into_status() { | ||
| let server_status = ServerStatus::new(StatusCode::Ok, "ok"); | ||
| let status = server_status.into_status(); | ||
| assert_eq!(status.code(), StatusCode::Ok); | ||
| } | ||
| } |
This file contains hidden or 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,22 @@ | ||
| /// Represents a gRPC status code. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| #[repr(i32)] | ||
| pub enum StatusCode { | ||
| Ok = 0, | ||
| Cancelled = 1, | ||
| Unknown = 2, | ||
| InvalidArgument = 3, | ||
| DeadlineExceeded = 4, | ||
| NotFound = 5, | ||
| AlreadyExists = 6, | ||
| PermissionDenied = 7, | ||
| ResourceExhausted = 8, | ||
| FailedPrecondition = 9, | ||
| Aborted = 10, | ||
| OutOfRange = 11, | ||
| Unimplemented = 12, | ||
| Internal = 13, | ||
| Unavailable = 14, | ||
| DataLoss = 15, | ||
| Unauthenticated = 16, | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.