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 (PreconditionFailure) #1276

Merged
merged 3 commits into from
Feb 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tonic-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub use pb::Status;
mod richer_error;

pub use richer_error::{
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation, QuotaFailure,
QuotaViolation, RetryInfo, StatusExt,
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation,
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RetryInfo, StatusExt,
};

mod sealed {
Expand Down
201 changes: 182 additions & 19 deletions tonic-types/src/richer_error/error_details/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{collections::HashMap, time};

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

pub(crate) mod vec;
Expand All @@ -25,6 +26,9 @@ pub struct ErrorDetails {
/// This field stores [`ErrorInfo`] data, if any.
pub(crate) error_info: Option<ErrorInfo>,

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

/// This field stores [`BadRequest`] data, if any.
pub(crate) bad_request: Option<BadRequest>,
}
Expand All @@ -35,7 +39,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let err_details = ErrorDetails::new();
/// ```
Expand All @@ -50,7 +54,7 @@ impl ErrorDetails {
///
/// ```
/// use std::time::Duration;
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let err_details = ErrorDetails::with_retry_info(Some(Duration::from_secs(5)));
/// ```
Expand All @@ -67,7 +71,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let err_stack = vec!["...".into(), "...".into()];
///
Expand Down Expand Up @@ -106,7 +110,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let err_details = ErrorDetails::with_quota_failure_violation("subject", "description");
/// ```
Expand All @@ -127,7 +131,7 @@ impl ErrorDetails {
///
/// ```
/// use std::collections::HashMap;
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut metadata: HashMap<String, String> = HashMap::new();
/// metadata.insert("instanceLimitPerRequest".into(), "100".into());
Expand All @@ -145,6 +149,64 @@ impl ErrorDetails {
}
}

/// Generates an [`ErrorDetails`] struct with [`PreconditionFailure`]
/// details and remaining fields set to `None`.
///
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails, PreconditionViolation};
///
/// let err_details = ErrorDetails::with_precondition_failure(vec![
/// PreconditionViolation::new(
/// "violation type 1",
/// "subject 1",
/// "description 1",
/// ),
/// PreconditionViolation::new(
/// "violation type 2",
/// "subject 2",
/// "description 2",
/// ),
/// ]);
/// ```
pub fn with_precondition_failure(violations: Vec<PreconditionViolation>) -> Self {
ErrorDetails {
precondition_failure: Some(PreconditionFailure::new(violations)),
..ErrorDetails::new()
}
}

/// Generates an [`ErrorDetails`] struct with [`PreconditionFailure`]
/// details (one [`PreconditionViolation`] set) and remaining fields set to
/// `None`.
///
/// # Examples
///
/// ```
/// use tonic_types::ErrorDetails;
///
/// let err_details = ErrorDetails::with_precondition_failure_violation(
/// "violation type",
/// "subject",
/// "description",
/// );
/// ```
pub fn with_precondition_failure_violation(
violation_type: impl Into<String>,
subject: impl Into<String>,
description: impl Into<String>,
) -> Self {
ErrorDetails {
precondition_failure: Some(PreconditionFailure::with_violation(
violation_type,
subject,
description,
)),
..ErrorDetails::new()
}
}

/// Generates an [`ErrorDetails`] struct with [`BadRequest`] details and
/// remaining fields set to `None`.
///
Expand All @@ -171,7 +233,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let err_details = ErrorDetails::with_bad_request_violation(
/// "field",
Expand All @@ -188,27 +250,32 @@ impl ErrorDetails {
}
}

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

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

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

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

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

/// Get [`BadRequest`] details, if any.
pub fn bad_request(&self) -> Option<BadRequest> {
self.bad_request.clone()
}
Expand All @@ -220,7 +287,7 @@ impl ErrorDetails {
///
/// ```
/// use std::time::Duration;
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
Expand All @@ -237,7 +304,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
Expand Down Expand Up @@ -281,7 +348,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
Expand Down Expand Up @@ -309,7 +376,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::with_quota_failure(vec![]);
///
Expand All @@ -333,7 +400,7 @@ impl ErrorDetails {
///
/// ```
/// use std::collections::HashMap;
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
Expand All @@ -352,6 +419,102 @@ impl ErrorDetails {
self
}

/// Set [`PreconditionFailure`] details. Can be chained with other `.set_`
/// and `.add_` [`ErrorDetails`] methods.
///
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails, PreconditionViolation};
///
/// let mut err_details = ErrorDetails::new();
///
/// err_details.set_precondition_failure(vec![
/// PreconditionViolation::new(
/// "violation type 1",
/// "subject 1",
/// "description 1",
/// ),
/// PreconditionViolation::new(
/// "violation type 2",
/// "subject 2",
/// "description 2",
/// ),
/// ]);
/// ```
pub fn set_precondition_failure(
&mut self,
violations: Vec<PreconditionViolation>,
) -> &mut Self {
self.precondition_failure = Some(PreconditionFailure::new(violations));
self
}

/// Adds a [`PreconditionViolation`] to [`PreconditionFailure`] details.
/// Sets [`PreconditionFailure`] details if it is not set yet. Can be
/// chained with other `.set_` and `.add_` [`ErrorDetails`] methods.
///
/// # Examples
///
/// ```
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
/// err_details.add_precondition_failure_violation(
/// "violation type",
/// "subject",
/// "description"
/// );
/// ```
pub fn add_precondition_failure_violation(
&mut self,
violation_type: impl Into<String>,
subject: impl Into<String>,
description: impl Into<String>,
) -> &mut Self {
match &mut self.precondition_failure {
Some(precondition_failure) => {
precondition_failure.add_violation(violation_type, subject, description);
}
None => {
self.precondition_failure = Some(PreconditionFailure::with_violation(
violation_type,
subject,
description,
));
}
};
self
}

/// Returns `true` if [`PreconditionFailure`] is set and its `violations`
/// vector is not empty, otherwise returns `false`.
///
/// # Examples
///
/// ```
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::with_precondition_failure(vec![]);
///
/// assert_eq!(err_details.has_precondition_failure_violations(), false);
///
/// err_details.add_precondition_failure_violation(
/// "violation type",
/// "subject",
/// "description"
/// );
///
/// assert_eq!(err_details.has_precondition_failure_violations(), true);
/// ```
pub fn has_precondition_failure_violations(&self) -> bool {
if let Some(precondition_failure) = &self.precondition_failure {
return !precondition_failure.violations.is_empty();
}
false
}

/// Set [`BadRequest`] details. Can be chained with other `.set_` and
/// `.add_` [`ErrorDetails`] methods.
///
Expand Down Expand Up @@ -379,7 +542,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::new();
///
Expand Down Expand Up @@ -407,7 +570,7 @@ impl ErrorDetails {
/// # Examples
///
/// ```
/// use tonic_types::{ErrorDetails};
/// use tonic_types::ErrorDetails;
///
/// let mut err_details = ErrorDetails::with_bad_request(vec![]);
///
Expand Down
13 changes: 12 additions & 1 deletion tonic-types/src/richer_error/error_details/vec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::super::std_messages::{BadRequest, DebugInfo, ErrorInfo, QuotaFailure, RetryInfo};
use super::super::std_messages::{
BadRequest, DebugInfo, ErrorInfo, PreconditionFailure, QuotaFailure, RetryInfo,
};

/// Wraps the structs corresponding to the standard error messages, allowing
/// the implementation and handling of vectors containing any of them.
Expand All @@ -17,6 +19,9 @@ pub enum ErrorDetail {
/// Wraps the [`ErrorInfo`] struct.
ErrorInfo(ErrorInfo),

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

/// Wraps the [`BadRequest`] struct.
BadRequest(BadRequest),
}
Expand Down Expand Up @@ -45,6 +50,12 @@ impl From<ErrorInfo> for ErrorDetail {
}
}

impl From<PreconditionFailure> for ErrorDetail {
fn from(err_detail: PreconditionFailure) -> Self {
ErrorDetail::PreconditionFailure(err_detail)
}
}

impl From<BadRequest> for ErrorDetail {
fn from(err_detail: BadRequest) -> Self {
ErrorDetail::BadRequest(err_detail)
Expand Down
Loading