Skip to content

Commit 2378581

Browse files
authored
feat(types): Add gRPC Richer Error Model support (PreconditionFailure) (#1276)
* types: add support for `PreconditionFailure` error message type Following implementation at flemosr/tonic-richer-error. * types: doc comments nits * types: merge `impl` blocks at `std_messages`
1 parent 555a8bc commit 2378581

File tree

10 files changed

+465
-39
lines changed

10 files changed

+465
-39
lines changed

tonic-types/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub use pb::Status;
4646
mod richer_error;
4747

4848
pub use richer_error::{
49-
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation, QuotaFailure,
50-
QuotaViolation, RetryInfo, StatusExt,
49+
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation,
50+
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RetryInfo, StatusExt,
5151
};
5252

5353
mod sealed {

tonic-types/src/richer_error/error_details/mod.rs

+182-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::{collections::HashMap, time};
22

33
use super::std_messages::{
4-
BadRequest, DebugInfo, ErrorInfo, FieldViolation, QuotaFailure, QuotaViolation, RetryInfo,
4+
BadRequest, DebugInfo, ErrorInfo, FieldViolation, PreconditionFailure, PreconditionViolation,
5+
QuotaFailure, QuotaViolation, RetryInfo,
56
};
67

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

29+
/// This field stores [`PreconditionFailure`] data, if any.
30+
pub(crate) precondition_failure: Option<PreconditionFailure>,
31+
2832
/// This field stores [`BadRequest`] data, if any.
2933
pub(crate) bad_request: Option<BadRequest>,
3034
}
@@ -35,7 +39,7 @@ impl ErrorDetails {
3539
/// # Examples
3640
///
3741
/// ```
38-
/// use tonic_types::{ErrorDetails};
42+
/// use tonic_types::ErrorDetails;
3943
///
4044
/// let err_details = ErrorDetails::new();
4145
/// ```
@@ -50,7 +54,7 @@ impl ErrorDetails {
5054
///
5155
/// ```
5256
/// use std::time::Duration;
53-
/// use tonic_types::{ErrorDetails};
57+
/// use tonic_types::ErrorDetails;
5458
///
5559
/// let err_details = ErrorDetails::with_retry_info(Some(Duration::from_secs(5)));
5660
/// ```
@@ -67,7 +71,7 @@ impl ErrorDetails {
6771
/// # Examples
6872
///
6973
/// ```
70-
/// use tonic_types::{ErrorDetails};
74+
/// use tonic_types::ErrorDetails;
7175
///
7276
/// let err_stack = vec!["...".into(), "...".into()];
7377
///
@@ -106,7 +110,7 @@ impl ErrorDetails {
106110
/// # Examples
107111
///
108112
/// ```
109-
/// use tonic_types::{ErrorDetails};
113+
/// use tonic_types::ErrorDetails;
110114
///
111115
/// let err_details = ErrorDetails::with_quota_failure_violation("subject", "description");
112116
/// ```
@@ -127,7 +131,7 @@ impl ErrorDetails {
127131
///
128132
/// ```
129133
/// use std::collections::HashMap;
130-
/// use tonic_types::{ErrorDetails};
134+
/// use tonic_types::ErrorDetails;
131135
///
132136
/// let mut metadata: HashMap<String, String> = HashMap::new();
133137
/// metadata.insert("instanceLimitPerRequest".into(), "100".into());
@@ -145,6 +149,64 @@ impl ErrorDetails {
145149
}
146150
}
147151

152+
/// Generates an [`ErrorDetails`] struct with [`PreconditionFailure`]
153+
/// details and remaining fields set to `None`.
154+
///
155+
/// # Examples
156+
///
157+
/// ```
158+
/// use tonic_types::{ErrorDetails, PreconditionViolation};
159+
///
160+
/// let err_details = ErrorDetails::with_precondition_failure(vec![
161+
/// PreconditionViolation::new(
162+
/// "violation type 1",
163+
/// "subject 1",
164+
/// "description 1",
165+
/// ),
166+
/// PreconditionViolation::new(
167+
/// "violation type 2",
168+
/// "subject 2",
169+
/// "description 2",
170+
/// ),
171+
/// ]);
172+
/// ```
173+
pub fn with_precondition_failure(violations: Vec<PreconditionViolation>) -> Self {
174+
ErrorDetails {
175+
precondition_failure: Some(PreconditionFailure::new(violations)),
176+
..ErrorDetails::new()
177+
}
178+
}
179+
180+
/// Generates an [`ErrorDetails`] struct with [`PreconditionFailure`]
181+
/// details (one [`PreconditionViolation`] set) and remaining fields set to
182+
/// `None`.
183+
///
184+
/// # Examples
185+
///
186+
/// ```
187+
/// use tonic_types::ErrorDetails;
188+
///
189+
/// let err_details = ErrorDetails::with_precondition_failure_violation(
190+
/// "violation type",
191+
/// "subject",
192+
/// "description",
193+
/// );
194+
/// ```
195+
pub fn with_precondition_failure_violation(
196+
violation_type: impl Into<String>,
197+
subject: impl Into<String>,
198+
description: impl Into<String>,
199+
) -> Self {
200+
ErrorDetails {
201+
precondition_failure: Some(PreconditionFailure::with_violation(
202+
violation_type,
203+
subject,
204+
description,
205+
)),
206+
..ErrorDetails::new()
207+
}
208+
}
209+
148210
/// Generates an [`ErrorDetails`] struct with [`BadRequest`] details and
149211
/// remaining fields set to `None`.
150212
///
@@ -171,7 +233,7 @@ impl ErrorDetails {
171233
/// # Examples
172234
///
173235
/// ```
174-
/// use tonic_types::{ErrorDetails};
236+
/// use tonic_types::ErrorDetails;
175237
///
176238
/// let err_details = ErrorDetails::with_bad_request_violation(
177239
/// "field",
@@ -188,27 +250,32 @@ impl ErrorDetails {
188250
}
189251
}
190252

191-
/// Get [`RetryInfo`] details, if any
253+
/// Get [`RetryInfo`] details, if any.
192254
pub fn retry_info(&self) -> Option<RetryInfo> {
193255
self.retry_info.clone()
194256
}
195257

196-
/// Get [`DebugInfo`] details, if any
258+
/// Get [`DebugInfo`] details, if any.
197259
pub fn debug_info(&self) -> Option<DebugInfo> {
198260
self.debug_info.clone()
199261
}
200262

201-
/// Get [`QuotaFailure`] details, if any
263+
/// Get [`QuotaFailure`] details, if any.
202264
pub fn quota_failure(&self) -> Option<QuotaFailure> {
203265
self.quota_failure.clone()
204266
}
205267

206-
/// Get [`ErrorInfo`] details, if any
268+
/// Get [`ErrorInfo`] details, if any.
207269
pub fn error_info(&self) -> Option<ErrorInfo> {
208270
self.error_info.clone()
209271
}
210272

211-
/// Get [`BadRequest`] details, if any
273+
/// Get [`PreconditionFailure`] details, if any.
274+
pub fn precondition_failure(&self) -> Option<PreconditionFailure> {
275+
self.precondition_failure.clone()
276+
}
277+
278+
/// Get [`BadRequest`] details, if any.
212279
pub fn bad_request(&self) -> Option<BadRequest> {
213280
self.bad_request.clone()
214281
}
@@ -220,7 +287,7 @@ impl ErrorDetails {
220287
///
221288
/// ```
222289
/// use std::time::Duration;
223-
/// use tonic_types::{ErrorDetails};
290+
/// use tonic_types::ErrorDetails;
224291
///
225292
/// let mut err_details = ErrorDetails::new();
226293
///
@@ -237,7 +304,7 @@ impl ErrorDetails {
237304
/// # Examples
238305
///
239306
/// ```
240-
/// use tonic_types::{ErrorDetails};
307+
/// use tonic_types::ErrorDetails;
241308
///
242309
/// let mut err_details = ErrorDetails::new();
243310
///
@@ -281,7 +348,7 @@ impl ErrorDetails {
281348
/// # Examples
282349
///
283350
/// ```
284-
/// use tonic_types::{ErrorDetails};
351+
/// use tonic_types::ErrorDetails;
285352
///
286353
/// let mut err_details = ErrorDetails::new();
287354
///
@@ -309,7 +376,7 @@ impl ErrorDetails {
309376
/// # Examples
310377
///
311378
/// ```
312-
/// use tonic_types::{ErrorDetails};
379+
/// use tonic_types::ErrorDetails;
313380
///
314381
/// let mut err_details = ErrorDetails::with_quota_failure(vec![]);
315382
///
@@ -333,7 +400,7 @@ impl ErrorDetails {
333400
///
334401
/// ```
335402
/// use std::collections::HashMap;
336-
/// use tonic_types::{ErrorDetails};
403+
/// use tonic_types::ErrorDetails;
337404
///
338405
/// let mut err_details = ErrorDetails::new();
339406
///
@@ -352,6 +419,102 @@ impl ErrorDetails {
352419
self
353420
}
354421

422+
/// Set [`PreconditionFailure`] details. Can be chained with other `.set_`
423+
/// and `.add_` [`ErrorDetails`] methods.
424+
///
425+
/// # Examples
426+
///
427+
/// ```
428+
/// use tonic_types::{ErrorDetails, PreconditionViolation};
429+
///
430+
/// let mut err_details = ErrorDetails::new();
431+
///
432+
/// err_details.set_precondition_failure(vec![
433+
/// PreconditionViolation::new(
434+
/// "violation type 1",
435+
/// "subject 1",
436+
/// "description 1",
437+
/// ),
438+
/// PreconditionViolation::new(
439+
/// "violation type 2",
440+
/// "subject 2",
441+
/// "description 2",
442+
/// ),
443+
/// ]);
444+
/// ```
445+
pub fn set_precondition_failure(
446+
&mut self,
447+
violations: Vec<PreconditionViolation>,
448+
) -> &mut Self {
449+
self.precondition_failure = Some(PreconditionFailure::new(violations));
450+
self
451+
}
452+
453+
/// Adds a [`PreconditionViolation`] to [`PreconditionFailure`] details.
454+
/// Sets [`PreconditionFailure`] details if it is not set yet. Can be
455+
/// chained with other `.set_` and `.add_` [`ErrorDetails`] methods.
456+
///
457+
/// # Examples
458+
///
459+
/// ```
460+
/// use tonic_types::ErrorDetails;
461+
///
462+
/// let mut err_details = ErrorDetails::new();
463+
///
464+
/// err_details.add_precondition_failure_violation(
465+
/// "violation type",
466+
/// "subject",
467+
/// "description"
468+
/// );
469+
/// ```
470+
pub fn add_precondition_failure_violation(
471+
&mut self,
472+
violation_type: impl Into<String>,
473+
subject: impl Into<String>,
474+
description: impl Into<String>,
475+
) -> &mut Self {
476+
match &mut self.precondition_failure {
477+
Some(precondition_failure) => {
478+
precondition_failure.add_violation(violation_type, subject, description);
479+
}
480+
None => {
481+
self.precondition_failure = Some(PreconditionFailure::with_violation(
482+
violation_type,
483+
subject,
484+
description,
485+
));
486+
}
487+
};
488+
self
489+
}
490+
491+
/// Returns `true` if [`PreconditionFailure`] is set and its `violations`
492+
/// vector is not empty, otherwise returns `false`.
493+
///
494+
/// # Examples
495+
///
496+
/// ```
497+
/// use tonic_types::ErrorDetails;
498+
///
499+
/// let mut err_details = ErrorDetails::with_precondition_failure(vec![]);
500+
///
501+
/// assert_eq!(err_details.has_precondition_failure_violations(), false);
502+
///
503+
/// err_details.add_precondition_failure_violation(
504+
/// "violation type",
505+
/// "subject",
506+
/// "description"
507+
/// );
508+
///
509+
/// assert_eq!(err_details.has_precondition_failure_violations(), true);
510+
/// ```
511+
pub fn has_precondition_failure_violations(&self) -> bool {
512+
if let Some(precondition_failure) = &self.precondition_failure {
513+
return !precondition_failure.violations.is_empty();
514+
}
515+
false
516+
}
517+
355518
/// Set [`BadRequest`] details. Can be chained with other `.set_` and
356519
/// `.add_` [`ErrorDetails`] methods.
357520
///
@@ -379,7 +542,7 @@ impl ErrorDetails {
379542
/// # Examples
380543
///
381544
/// ```
382-
/// use tonic_types::{ErrorDetails};
545+
/// use tonic_types::ErrorDetails;
383546
///
384547
/// let mut err_details = ErrorDetails::new();
385548
///
@@ -407,7 +570,7 @@ impl ErrorDetails {
407570
/// # Examples
408571
///
409572
/// ```
410-
/// use tonic_types::{ErrorDetails};
573+
/// use tonic_types::ErrorDetails;
411574
///
412575
/// let mut err_details = ErrorDetails::with_bad_request(vec![]);
413576
///

tonic-types/src/richer_error/error_details/vec.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::super::std_messages::{BadRequest, DebugInfo, ErrorInfo, QuotaFailure, RetryInfo};
1+
use super::super::std_messages::{
2+
BadRequest, DebugInfo, ErrorInfo, PreconditionFailure, QuotaFailure, RetryInfo,
3+
};
24

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

22+
/// Wraps the [`PreconditionFailure`] struct.
23+
PreconditionFailure(PreconditionFailure),
24+
2025
/// Wraps the [`BadRequest`] struct.
2126
BadRequest(BadRequest),
2227
}
@@ -45,6 +50,12 @@ impl From<ErrorInfo> for ErrorDetail {
4550
}
4651
}
4752

53+
impl From<PreconditionFailure> for ErrorDetail {
54+
fn from(err_detail: PreconditionFailure) -> Self {
55+
ErrorDetail::PreconditionFailure(err_detail)
56+
}
57+
}
58+
4859
impl From<BadRequest> for ErrorDetail {
4960
fn from(err_detail: BadRequest) -> Self {
5061
ErrorDetail::BadRequest(err_detail)

0 commit comments

Comments
 (0)