Skip to content

Commit 2378581

Browse files
authoredFeb 16, 2023
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)

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

+62-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use super::pb;
1212

1313
pub use error_details::{vec::ErrorDetail, ErrorDetails};
1414
pub use std_messages::{
15-
BadRequest, DebugInfo, ErrorInfo, FieldViolation, QuotaFailure, QuotaViolation, RetryInfo,
15+
BadRequest, DebugInfo, ErrorInfo, FieldViolation, PreconditionFailure, PreconditionViolation,
16+
QuotaFailure, QuotaViolation, RetryInfo,
1617
};
1718

1819
trait IntoAny {
@@ -147,7 +148,7 @@ pub trait StatusExt: crate::sealed::Sealed {
147148
///
148149
/// ```
149150
/// use tonic::{Status, Response};
150-
/// use tonic_types::{StatusExt};
151+
/// use tonic_types::StatusExt;
151152
///
152153
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
153154
/// match req_result {
@@ -172,7 +173,7 @@ pub trait StatusExt: crate::sealed::Sealed {
172173
///
173174
/// ```
174175
/// use tonic::{Status, Response};
175-
/// use tonic_types::{StatusExt};
176+
/// use tonic_types::StatusExt;
176177
///
177178
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
178179
/// match req_result {
@@ -256,7 +257,7 @@ pub trait StatusExt: crate::sealed::Sealed {
256257
///
257258
/// ```
258259
/// use tonic::{Status, Response};
259-
/// use tonic_types::{StatusExt};
260+
/// use tonic_types::StatusExt;
260261
///
261262
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
262263
/// match req_result {
@@ -278,7 +279,7 @@ pub trait StatusExt: crate::sealed::Sealed {
278279
///
279280
/// ```
280281
/// use tonic::{Status, Response};
281-
/// use tonic_types::{StatusExt};
282+
/// use tonic_types::StatusExt;
282283
///
283284
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
284285
/// match req_result {
@@ -300,7 +301,7 @@ pub trait StatusExt: crate::sealed::Sealed {
300301
///
301302
/// ```
302303
/// use tonic::{Status, Response};
303-
/// use tonic_types::{StatusExt};
304+
/// use tonic_types::StatusExt;
304305
///
305306
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
306307
/// match req_result {
@@ -322,7 +323,7 @@ pub trait StatusExt: crate::sealed::Sealed {
322323
///
323324
/// ```
324325
/// use tonic::{Status, Response};
325-
/// use tonic_types::{StatusExt};
326+
/// use tonic_types::StatusExt;
326327
///
327328
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
328329
/// match req_result {
@@ -337,14 +338,36 @@ pub trait StatusExt: crate::sealed::Sealed {
337338
/// ```
338339
fn get_details_error_info(&self) -> Option<ErrorInfo>;
339340

341+
/// Get first [`PreconditionFailure`] details found on `tonic::Status`,
342+
/// if any. If some `prost::DecodeError` occurs, returns `None`.
343+
///
344+
/// # Examples
345+
///
346+
/// ```
347+
/// use tonic::{Status, Response};
348+
/// use tonic_types::StatusExt;
349+
///
350+
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
351+
/// match req_result {
352+
/// Ok(_) => {},
353+
/// Err(status) => {
354+
/// if let Some(precondition_failure) = status.get_details_precondition_failure() {
355+
/// // Handle precondition_failure details
356+
/// }
357+
/// }
358+
/// };
359+
/// }
360+
/// ```
361+
fn get_details_precondition_failure(&self) -> Option<PreconditionFailure>;
362+
340363
/// Get first [`BadRequest`] details found on `tonic::Status`, if any. If
341364
/// some `prost::DecodeError` occurs, returns `None`.
342365
///
343366
/// # Examples
344367
///
345368
/// ```
346369
/// use tonic::{Status, Response};
347-
/// use tonic_types::{StatusExt};
370+
/// use tonic_types::StatusExt;
348371
///
349372
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
350373
/// match req_result {
@@ -389,6 +412,10 @@ impl StatusExt for tonic::Status {
389412
conv_details.push(error_info.into_any());
390413
}
391414

415+
if let Some(precondition_failure) = details.precondition_failure {
416+
conv_details.push(precondition_failure.into_any());
417+
}
418+
392419
if let Some(bad_request) = details.bad_request {
393420
conv_details.push(bad_request.into_any());
394421
}
@@ -426,6 +453,9 @@ impl StatusExt for tonic::Status {
426453
ErrorDetail::ErrorInfo(error_info) => {
427454
conv_details.push(error_info.into_any());
428455
}
456+
ErrorDetail::PreconditionFailure(prec_failure) => {
457+
conv_details.push(prec_failure.into_any());
458+
}
429459
ErrorDetail::BadRequest(bad_req) => {
430460
conv_details.push(bad_req.into_any());
431461
}
@@ -469,6 +499,9 @@ impl StatusExt for tonic::Status {
469499
ErrorInfo::TYPE_URL => {
470500
details.error_info = Some(ErrorInfo::from_any(any)?);
471501
}
502+
PreconditionFailure::TYPE_URL => {
503+
details.precondition_failure = Some(PreconditionFailure::from_any(any)?);
504+
}
472505
BadRequest::TYPE_URL => {
473506
details.bad_request = Some(BadRequest::from_any(any)?);
474507
}
@@ -502,6 +535,9 @@ impl StatusExt for tonic::Status {
502535
ErrorInfo::TYPE_URL => {
503536
details.push(ErrorInfo::from_any(any)?.into());
504537
}
538+
PreconditionFailure::TYPE_URL => {
539+
details.push(PreconditionFailure::from_any(any)?.into());
540+
}
505541
BadRequest::TYPE_URL => {
506542
details.push(BadRequest::from_any(any)?.into());
507543
}
@@ -572,6 +608,20 @@ impl StatusExt for tonic::Status {
572608
None
573609
}
574610

611+
fn get_details_precondition_failure(&self) -> Option<PreconditionFailure> {
612+
let status = pb::Status::decode(self.details()).ok()?;
613+
614+
for any in status.details.into_iter() {
615+
if any.type_url.as_str() == PreconditionFailure::TYPE_URL {
616+
if let Ok(detail) = PreconditionFailure::from_any(any) {
617+
return Some(detail);
618+
}
619+
}
620+
}
621+
622+
None
623+
}
624+
575625
fn get_details_bad_request(&self) -> Option<BadRequest> {
576626
let status = pb::Status::decode(self.details()).ok()?;
577627

@@ -593,7 +643,8 @@ mod tests {
593643
use tonic::{Code, Status};
594644

595645
use super::{
596-
BadRequest, DebugInfo, ErrorDetails, ErrorInfo, QuotaFailure, RetryInfo, StatusExt,
646+
BadRequest, DebugInfo, ErrorDetails, ErrorInfo, PreconditionFailure, QuotaFailure,
647+
RetryInfo, StatusExt,
597648
};
598649

599650
#[test]
@@ -611,6 +662,7 @@ mod tests {
611662
)
612663
.add_quota_failure_violation("clientip:<ip address>", "description")
613664
.set_error_info("SOME_INFO", "example.local", metadata.clone())
665+
.add_precondition_failure_violation("TOS", "example.local", "description")
614666
.add_bad_request_violation("field", "description");
615667

616668
let fmt_details = format!("{:?}", err_details);
@@ -624,6 +676,7 @@ mod tests {
624676
.into(),
625677
QuotaFailure::with_violation("clientip:<ip address>", "description").into(),
626678
ErrorInfo::new("SOME_INFO", "example.local", metadata).into(),
679+
PreconditionFailure::with_violation("TOS", "example.local", "description").into(),
627680
BadRequest::with_violation("field", "description").into(),
628681
];
629682

‎tonic-types/src/richer_error/std_messages/debug_info.rs

-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ impl DebugInfo {
2727
detail: detail.into(),
2828
}
2929
}
30-
}
3130

32-
impl DebugInfo {
3331
/// Returns `true` if [`DebugInfo`] fields are empty, and `false` if they
3432
/// are not.
3533
pub fn is_empty(&self) -> bool {

‎tonic-types/src/richer_error/std_messages/error_info.rs

-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ impl ErrorInfo {
4343
metadata: metadata.into(),
4444
}
4545
}
46-
}
4746

48-
impl ErrorInfo {
4947
/// Returns `true` if [`ErrorInfo`] fields are empty, and `false` if they
5048
/// are not.
5149
pub fn is_empty(&self) -> bool {

‎tonic-types/src/richer_error/std_messages/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ mod error_info;
1414

1515
pub use error_info::ErrorInfo;
1616

17+
mod prec_failure;
18+
19+
pub use prec_failure::{PreconditionFailure, PreconditionViolation};
20+
1721
mod bad_request;
1822

1923
pub use bad_request::{BadRequest, FieldViolation};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
use prost::{DecodeError, Message};
2+
use prost_types::Any;
3+
4+
use super::super::{pb, FromAny, IntoAny};
5+
6+
/// Used at the `violations` field of the [`PreconditionFailure`] struct.
7+
/// Describes a single precondition failure.
8+
#[derive(Clone, Debug)]
9+
pub struct PreconditionViolation {
10+
/// Type of the PreconditionFailure. At [error_details.proto], the usage
11+
/// of a service-specific enum type is recommended. For example, "TOS" for
12+
/// a "Terms of Service" violation.
13+
///
14+
/// [error_details.proto]: https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto
15+
pub r#type: String,
16+
17+
/// Subject, relative to the type, that failed.
18+
pub subject: String,
19+
20+
/// A description of how the precondition failed.
21+
pub description: String,
22+
}
23+
24+
impl PreconditionViolation {
25+
/// Creates a new [`PreconditionViolation`] struct.
26+
pub fn new(
27+
r#type: impl Into<String>,
28+
subject: impl Into<String>,
29+
description: impl Into<String>,
30+
) -> Self {
31+
PreconditionViolation {
32+
r#type: r#type.into(),
33+
subject: subject.into(),
34+
description: description.into(),
35+
}
36+
}
37+
}
38+
39+
/// Used to encode/decode the `PreconditionFailure` standard error message
40+
/// described in [error_details.proto]. Describes what preconditions have
41+
/// failed.
42+
///
43+
/// [error_details.proto]: https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto
44+
#[derive(Clone, Debug)]
45+
pub struct PreconditionFailure {
46+
/// Describes all precondition violations of the request.
47+
pub violations: Vec<PreconditionViolation>,
48+
}
49+
50+
impl PreconditionFailure {
51+
/// Type URL of the `PreconditionFailure` standard error message type.
52+
pub const TYPE_URL: &'static str = "type.googleapis.com/google.rpc.PreconditionFailure";
53+
54+
/// Creates a new [`PreconditionFailure`] struct.
55+
pub fn new(violations: Vec<PreconditionViolation>) -> Self {
56+
PreconditionFailure { violations }
57+
}
58+
59+
/// Creates a new [`PreconditionFailure`] struct with a single
60+
/// [`PreconditionViolation`] in `violations`.
61+
pub fn with_violation(
62+
violation_type: impl Into<String>,
63+
subject: impl Into<String>,
64+
description: impl Into<String>,
65+
) -> Self {
66+
PreconditionFailure {
67+
violations: vec![PreconditionViolation {
68+
r#type: violation_type.into(),
69+
subject: subject.into(),
70+
description: description.into(),
71+
}],
72+
}
73+
}
74+
75+
/// Adds a [`PreconditionViolation`] to [`PreconditionFailure`]'s
76+
/// `violations` vector.
77+
pub fn add_violation(
78+
&mut self,
79+
r#type: impl Into<String>,
80+
subject: impl Into<String>,
81+
description: impl Into<String>,
82+
) -> &mut Self {
83+
self.violations.append(&mut vec![PreconditionViolation {
84+
r#type: r#type.into(),
85+
subject: subject.into(),
86+
description: description.into(),
87+
}]);
88+
self
89+
}
90+
91+
/// Returns `true` if [`PreconditionFailure`]'s `violations` vector is
92+
/// empty, and `false` if it is not.
93+
pub fn is_empty(&self) -> bool {
94+
self.violations.is_empty()
95+
}
96+
}
97+
98+
impl IntoAny for PreconditionFailure {
99+
fn into_any(self) -> Any {
100+
let detail_data = pb::PreconditionFailure {
101+
violations: self
102+
.violations
103+
.into_iter()
104+
.map(|v| pb::precondition_failure::Violation {
105+
r#type: v.r#type,
106+
subject: v.subject,
107+
description: v.description,
108+
})
109+
.collect(),
110+
};
111+
112+
Any {
113+
type_url: PreconditionFailure::TYPE_URL.to_string(),
114+
value: detail_data.encode_to_vec(),
115+
}
116+
}
117+
}
118+
119+
impl FromAny for PreconditionFailure {
120+
fn from_any(any: Any) -> Result<Self, DecodeError> {
121+
let buf: &[u8] = &any.value;
122+
let precondition_failure = pb::PreconditionFailure::decode(buf)?;
123+
124+
let precondition_failure = PreconditionFailure {
125+
violations: precondition_failure
126+
.violations
127+
.into_iter()
128+
.map(|v| PreconditionViolation {
129+
r#type: v.r#type,
130+
subject: v.subject,
131+
description: v.description,
132+
})
133+
.collect(),
134+
};
135+
136+
Ok(precondition_failure)
137+
}
138+
}
139+
140+
#[cfg(test)]
141+
mod tests {
142+
use super::super::super::{FromAny, IntoAny};
143+
use super::PreconditionFailure;
144+
145+
#[test]
146+
fn gen_prec_failure() {
147+
let mut prec_failure = PreconditionFailure::new(Vec::new());
148+
let formatted = format!("{:?}", prec_failure);
149+
150+
let expected = "PreconditionFailure { violations: [] }";
151+
152+
assert!(
153+
formatted.eq(expected),
154+
"empty PreconditionFailure differs from expected result"
155+
);
156+
157+
assert!(
158+
prec_failure.is_empty(),
159+
"empty PreconditionFailure returns 'false' from .is_empty()"
160+
);
161+
162+
prec_failure
163+
.add_violation("TOS", "example.local", "Terms of service not accepted")
164+
.add_violation("FNF", "example.local", "File not found");
165+
166+
let formatted = format!("{:?}", prec_failure);
167+
168+
let expected_filled = "PreconditionFailure { violations: [PreconditionViolation { type: \"TOS\", subject: \"example.local\", description: \"Terms of service not accepted\" }, PreconditionViolation { type: \"FNF\", subject: \"example.local\", description: \"File not found\" }] }";
169+
170+
assert!(
171+
formatted.eq(expected_filled),
172+
"filled PreconditionFailure differs from expected result"
173+
);
174+
175+
assert!(
176+
!prec_failure.is_empty(),
177+
"filled PreconditionFailure returns 'true' from .is_empty()"
178+
);
179+
180+
let gen_any = prec_failure.into_any();
181+
182+
let formatted = format!("{:?}", gen_any);
183+
184+
let expected = "Any { type_url: \"type.googleapis.com/google.rpc.PreconditionFailure\", value: [10, 51, 10, 3, 84, 79, 83, 18, 13, 101, 120, 97, 109, 112, 108, 101, 46, 108, 111, 99, 97, 108, 26, 29, 84, 101, 114, 109, 115, 32, 111, 102, 32, 115, 101, 114, 118, 105, 99, 101, 32, 110, 111, 116, 32, 97, 99, 99, 101, 112, 116, 101, 100, 10, 36, 10, 3, 70, 78, 70, 18, 13, 101, 120, 97, 109, 112, 108, 101, 46, 108, 111, 99, 97, 108, 26, 14, 70, 105, 108, 101, 32, 110, 111, 116, 32, 102, 111, 117, 110, 100] }";
185+
186+
assert!(
187+
formatted.eq(expected),
188+
"Any from filled PreconditionFailure differs from expected result"
189+
);
190+
191+
let br_details = match PreconditionFailure::from_any(gen_any) {
192+
Err(error) => panic!("Error generating PreconditionFailure from Any: {:?}", error),
193+
Ok(from_any) => from_any,
194+
};
195+
196+
let formatted = format!("{:?}", br_details);
197+
198+
assert!(
199+
formatted.eq(expected_filled),
200+
"PreconditionFailure from Any differs from expected result"
201+
);
202+
}
203+
}

‎tonic-types/src/richer_error/std_messages/quota_failure.rs

-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ impl QuotaFailure {
5353
}],
5454
}
5555
}
56-
}
5756

58-
impl QuotaFailure {
5957
/// Adds a [`QuotaViolation`] to [`QuotaFailure`]'s `violations`.
6058
pub fn add_violation(
6159
&mut self,

‎tonic-types/src/richer_error/std_messages/retry_info.rs

-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ impl RetryInfo {
4242

4343
RetryInfo { retry_delay }
4444
}
45-
}
4645

47-
impl RetryInfo {
4846
/// Returns `true` if [`RetryInfo`]'s `retry_delay` is set as `None`, and
4947
/// `false` if it is not.
5048
pub fn is_empty(&self) -> bool {

0 commit comments

Comments
 (0)
Please sign in to comment.