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

feat(types): Add gRPC Richer Error Model support (ResourceInfo) #1282

Merged
merged 3 commits into from
Feb 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tonic-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod richer_error;
pub use richer_error::{
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation,
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo,
RetryInfo, StatusExt,
ResourceInfo, RetryInfo, StatusExt,
};

mod sealed {
Expand Down
70 changes: 69 additions & 1 deletion tonic-types/src/richer_error/error_details/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, time};

use super::std_messages::{
BadRequest, DebugInfo, ErrorInfo, FieldViolation, PreconditionFailure, PreconditionViolation,
QuotaFailure, QuotaViolation, RequestInfo, RetryInfo,
QuotaFailure, QuotaViolation, RequestInfo, ResourceInfo, RetryInfo,
};

pub(crate) mod vec;
Expand Down Expand Up @@ -34,6 +34,9 @@ pub struct ErrorDetails {

/// This field stores [`RequestInfo`] data, if any.
pub(crate) request_info: Option<RequestInfo>,

/// This field stores [`ResourceInfo`] data, if any.
pub(crate) resource_info: Option<ResourceInfo>,
}

impl ErrorDetails {
Expand Down Expand Up @@ -276,6 +279,38 @@ impl ErrorDetails {
}
}

/// Generates an [`ErrorDetails`] struct with [`ResourceInfo`] details and
/// remaining fields set to `None`.
///
/// # Examples
///
/// ```
/// use tonic_types::ErrorDetails;
///
/// let err_details = ErrorDetails::with_resource_info(
/// "res_type",
/// "res_name",
/// "owner",
/// "description",
/// );
/// ```
pub fn with_resource_info(
resource_type: impl Into<String>,
resource_name: impl Into<String>,
owner: impl Into<String>,
description: impl Into<String>,
) -> Self {
ErrorDetails {
resource_info: Some(ResourceInfo::new(
resource_type,
resource_name,
owner,
description,
)),
..ErrorDetails::new()
}
}

/// Get [`RetryInfo`] details, if any.
pub fn retry_info(&self) -> Option<RetryInfo> {
self.retry_info.clone()
Expand Down Expand Up @@ -311,6 +346,11 @@ impl ErrorDetails {
self.request_info.clone()
}

/// Get [`ResourceInfo`] details, if any.
pub fn resource_info(&self) -> Option<ResourceInfo> {
self.resource_info.clone()
}

/// Set [`RetryInfo`] details. Can be chained with other `.set_` and
/// `.add_` [`ErrorDetails`] methods.
///
Expand Down Expand Up @@ -638,4 +678,32 @@ impl ErrorDetails {
self.request_info = Some(RequestInfo::new(request_id, serving_data));
self
}

/// Set [`ResourceInfo`] details. Can be chained with other `.set_` and
/// `.add_` [`ErrorDetails`] methods.
///
/// # Examples
///
/// ```
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
/// err_details.set_resource_info("res_type", "res_name", "owner", "description");
/// ```
pub fn set_resource_info(
&mut self,
resource_type: impl Into<String>,
resource_name: impl Into<String>,
owner: impl Into<String>,
description: impl Into<String>,
) -> &mut Self {
self.resource_info = Some(ResourceInfo::new(
resource_type,
resource_name,
owner,
description,
));
self
}
}
12 changes: 11 additions & 1 deletion tonic-types/src/richer_error/error_details/vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::super::std_messages::{
BadRequest, DebugInfo, ErrorInfo, PreconditionFailure, QuotaFailure, RequestInfo, RetryInfo,
BadRequest, DebugInfo, ErrorInfo, PreconditionFailure, QuotaFailure, RequestInfo, ResourceInfo,
RetryInfo,
};

/// Wraps the structs corresponding to the standard error messages, allowing
Expand Down Expand Up @@ -27,6 +28,9 @@ pub enum ErrorDetail {

/// Wraps the [`RequestInfo`] struct.
RequestInfo(RequestInfo),

/// Wraps the [`ResourceInfo`] struct.
ResourceInfo(ResourceInfo),
}

impl From<RetryInfo> for ErrorDetail {
Expand Down Expand Up @@ -70,3 +74,9 @@ impl From<RequestInfo> for ErrorDetail {
ErrorDetail::RequestInfo(err_detail)
}
}

impl From<ResourceInfo> for ErrorDetail {
fn from(err_detail: ResourceInfo) -> Self {
ErrorDetail::ResourceInfo(err_detail)
}
}
78 changes: 63 additions & 15 deletions tonic-types/src/richer_error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::pb;
pub use error_details::{vec::ErrorDetail, ErrorDetails};
pub use std_messages::{
BadRequest, DebugInfo, ErrorInfo, FieldViolation, PreconditionFailure, PreconditionViolation,
QuotaFailure, QuotaViolation, RequestInfo, RetryInfo,
QuotaFailure, QuotaViolation, RequestInfo, ResourceInfo, RetryInfo,
};

trait IntoAny {
Expand Down Expand Up @@ -153,12 +153,13 @@ pub trait StatusExt: crate::sealed::Sealed {
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
/// match req_result {
/// Ok(_) => {},
/// Err(status) => {
/// let err_details = status.get_error_details();
/// if let Some(bad_request) = err_details.bad_request() {
/// // Handle bad_request details
/// Err(status) => match status.check_error_details() {
/// Ok(err_details) => {
/// // Handle extracted details
/// }
/// Err(decode_error) => {
/// // Handle decode_error
/// }
/// // ...
/// }
/// };
/// }
Expand Down Expand Up @@ -201,19 +202,17 @@ pub trait StatusExt: crate::sealed::Sealed {
///
/// ```
/// use tonic::{Status, Response};
/// use tonic_types::{ErrorDetail, StatusExt};
/// use tonic_types::StatusExt;
///
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
/// match req_result {
/// Ok(_) => {},
/// Err(status) => {
/// match status.check_error_details_vec() {
/// Ok(err_details) => {
/// // Handle extracted details
/// }
/// Err(decode_error) => {
/// // Handle decode_error
/// }
/// Err(status) => match status.check_error_details_vec() {
/// Ok(err_details) => {
/// // Handle extracted details
/// }
/// Err(decode_error) => {
/// // Handle decode_error
/// }
/// }
/// };
Expand Down Expand Up @@ -403,6 +402,28 @@ pub trait StatusExt: crate::sealed::Sealed {
/// }
/// ```
fn get_details_request_info(&self) -> Option<RequestInfo>;

/// Get first [`ResourceInfo`] details found on `tonic::Status`, if any.
/// If some `prost::DecodeError` occurs, returns `None`.
///
/// # Examples
///
/// ```
/// use tonic::{Status, Response};
/// use tonic_types::StatusExt;
///
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
/// match req_result {
/// Ok(_) => {},
/// Err(status) => {
/// if let Some(resource_info) = status.get_details_resource_info() {
/// // Handle resource_info details
/// }
/// }
/// };
/// }
/// ```
fn get_details_resource_info(&self) -> Option<ResourceInfo>;
}

impl crate::sealed::Sealed for tonic::Status {}
Expand Down Expand Up @@ -446,6 +467,10 @@ impl StatusExt for tonic::Status {
conv_details.push(request_info.into_any());
}

if let Some(resource_info) = details.resource_info {
conv_details.push(resource_info.into_any());
}

let details = gen_details_bytes(code, &message, conv_details);

tonic::Status::with_details_and_metadata(code, message, details, metadata)
Expand Down Expand Up @@ -488,6 +513,9 @@ impl StatusExt for tonic::Status {
ErrorDetail::RequestInfo(req_info) => {
conv_details.push(req_info.into_any());
}
ErrorDetail::ResourceInfo(res_info) => {
conv_details.push(res_info.into_any());
}
}
}

Expand Down Expand Up @@ -537,6 +565,9 @@ impl StatusExt for tonic::Status {
RequestInfo::TYPE_URL => {
details.request_info = Some(RequestInfo::from_any(any)?);
}
ResourceInfo::TYPE_URL => {
details.resource_info = Some(ResourceInfo::from_any(any)?);
}
_ => {}
}
}
Expand Down Expand Up @@ -576,6 +607,9 @@ impl StatusExt for tonic::Status {
RequestInfo::TYPE_URL => {
details.push(RequestInfo::from_any(any)?.into());
}
ResourceInfo::TYPE_URL => {
details.push(ResourceInfo::from_any(any)?.into());
}
_ => {}
}
}
Expand Down Expand Up @@ -684,6 +718,20 @@ impl StatusExt for tonic::Status {

None
}

fn get_details_resource_info(&self) -> Option<ResourceInfo> {
let status = pb::Status::decode(self.details()).ok()?;

for any in status.details.into_iter() {
if any.type_url.as_str() == ResourceInfo::TYPE_URL {
if let Ok(detail) = ResourceInfo::from_any(any) {
return Some(detail);
}
}
}

None
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions tonic-types/src/richer_error/std_messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ pub use bad_request::{BadRequest, FieldViolation};
mod request_info;

pub use request_info::RequestInfo;

mod resource_info;

pub use resource_info::ResourceInfo;