1
1
use std:: { collections:: HashMap , time} ;
2
2
3
3
use super :: std_messages:: {
4
- BadRequest , DebugInfo , ErrorInfo , FieldViolation , QuotaFailure , QuotaViolation , RetryInfo ,
4
+ BadRequest , DebugInfo , ErrorInfo , FieldViolation , PreconditionFailure , PreconditionViolation ,
5
+ QuotaFailure , QuotaViolation , RetryInfo ,
5
6
} ;
6
7
7
8
pub ( crate ) mod vec;
@@ -25,6 +26,9 @@ pub struct ErrorDetails {
25
26
/// This field stores [`ErrorInfo`] data, if any.
26
27
pub ( crate ) error_info : Option < ErrorInfo > ,
27
28
29
+ /// This field stores [`PreconditionFailure`] data, if any.
30
+ pub ( crate ) precondition_failure : Option < PreconditionFailure > ,
31
+
28
32
/// This field stores [`BadRequest`] data, if any.
29
33
pub ( crate ) bad_request : Option < BadRequest > ,
30
34
}
@@ -35,7 +39,7 @@ impl ErrorDetails {
35
39
/// # Examples
36
40
///
37
41
/// ```
38
- /// use tonic_types::{ ErrorDetails} ;
42
+ /// use tonic_types::ErrorDetails;
39
43
///
40
44
/// let err_details = ErrorDetails::new();
41
45
/// ```
@@ -50,7 +54,7 @@ impl ErrorDetails {
50
54
///
51
55
/// ```
52
56
/// use std::time::Duration;
53
- /// use tonic_types::{ ErrorDetails} ;
57
+ /// use tonic_types::ErrorDetails;
54
58
///
55
59
/// let err_details = ErrorDetails::with_retry_info(Some(Duration::from_secs(5)));
56
60
/// ```
@@ -67,7 +71,7 @@ impl ErrorDetails {
67
71
/// # Examples
68
72
///
69
73
/// ```
70
- /// use tonic_types::{ ErrorDetails} ;
74
+ /// use tonic_types::ErrorDetails;
71
75
///
72
76
/// let err_stack = vec!["...".into(), "...".into()];
73
77
///
@@ -106,7 +110,7 @@ impl ErrorDetails {
106
110
/// # Examples
107
111
///
108
112
/// ```
109
- /// use tonic_types::{ ErrorDetails} ;
113
+ /// use tonic_types::ErrorDetails;
110
114
///
111
115
/// let err_details = ErrorDetails::with_quota_failure_violation("subject", "description");
112
116
/// ```
@@ -127,7 +131,7 @@ impl ErrorDetails {
127
131
///
128
132
/// ```
129
133
/// use std::collections::HashMap;
130
- /// use tonic_types::{ ErrorDetails} ;
134
+ /// use tonic_types::ErrorDetails;
131
135
///
132
136
/// let mut metadata: HashMap<String, String> = HashMap::new();
133
137
/// metadata.insert("instanceLimitPerRequest".into(), "100".into());
@@ -145,6 +149,64 @@ impl ErrorDetails {
145
149
}
146
150
}
147
151
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
+
148
210
/// Generates an [`ErrorDetails`] struct with [`BadRequest`] details and
149
211
/// remaining fields set to `None`.
150
212
///
@@ -171,7 +233,7 @@ impl ErrorDetails {
171
233
/// # Examples
172
234
///
173
235
/// ```
174
- /// use tonic_types::{ ErrorDetails} ;
236
+ /// use tonic_types::ErrorDetails;
175
237
///
176
238
/// let err_details = ErrorDetails::with_bad_request_violation(
177
239
/// "field",
@@ -188,27 +250,32 @@ impl ErrorDetails {
188
250
}
189
251
}
190
252
191
- /// Get [`RetryInfo`] details, if any
253
+ /// Get [`RetryInfo`] details, if any.
192
254
pub fn retry_info ( & self ) -> Option < RetryInfo > {
193
255
self . retry_info . clone ( )
194
256
}
195
257
196
- /// Get [`DebugInfo`] details, if any
258
+ /// Get [`DebugInfo`] details, if any.
197
259
pub fn debug_info ( & self ) -> Option < DebugInfo > {
198
260
self . debug_info . clone ( )
199
261
}
200
262
201
- /// Get [`QuotaFailure`] details, if any
263
+ /// Get [`QuotaFailure`] details, if any.
202
264
pub fn quota_failure ( & self ) -> Option < QuotaFailure > {
203
265
self . quota_failure . clone ( )
204
266
}
205
267
206
- /// Get [`ErrorInfo`] details, if any
268
+ /// Get [`ErrorInfo`] details, if any.
207
269
pub fn error_info ( & self ) -> Option < ErrorInfo > {
208
270
self . error_info . clone ( )
209
271
}
210
272
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.
212
279
pub fn bad_request ( & self ) -> Option < BadRequest > {
213
280
self . bad_request . clone ( )
214
281
}
@@ -220,7 +287,7 @@ impl ErrorDetails {
220
287
///
221
288
/// ```
222
289
/// use std::time::Duration;
223
- /// use tonic_types::{ ErrorDetails} ;
290
+ /// use tonic_types::ErrorDetails;
224
291
///
225
292
/// let mut err_details = ErrorDetails::new();
226
293
///
@@ -237,7 +304,7 @@ impl ErrorDetails {
237
304
/// # Examples
238
305
///
239
306
/// ```
240
- /// use tonic_types::{ ErrorDetails} ;
307
+ /// use tonic_types::ErrorDetails;
241
308
///
242
309
/// let mut err_details = ErrorDetails::new();
243
310
///
@@ -281,7 +348,7 @@ impl ErrorDetails {
281
348
/// # Examples
282
349
///
283
350
/// ```
284
- /// use tonic_types::{ ErrorDetails} ;
351
+ /// use tonic_types::ErrorDetails;
285
352
///
286
353
/// let mut err_details = ErrorDetails::new();
287
354
///
@@ -309,7 +376,7 @@ impl ErrorDetails {
309
376
/// # Examples
310
377
///
311
378
/// ```
312
- /// use tonic_types::{ ErrorDetails} ;
379
+ /// use tonic_types::ErrorDetails;
313
380
///
314
381
/// let mut err_details = ErrorDetails::with_quota_failure(vec![]);
315
382
///
@@ -333,7 +400,7 @@ impl ErrorDetails {
333
400
///
334
401
/// ```
335
402
/// use std::collections::HashMap;
336
- /// use tonic_types::{ ErrorDetails} ;
403
+ /// use tonic_types::ErrorDetails;
337
404
///
338
405
/// let mut err_details = ErrorDetails::new();
339
406
///
@@ -352,6 +419,102 @@ impl ErrorDetails {
352
419
self
353
420
}
354
421
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
+
355
518
/// Set [`BadRequest`] details. Can be chained with other `.set_` and
356
519
/// `.add_` [`ErrorDetails`] methods.
357
520
///
@@ -379,7 +542,7 @@ impl ErrorDetails {
379
542
/// # Examples
380
543
///
381
544
/// ```
382
- /// use tonic_types::{ ErrorDetails} ;
545
+ /// use tonic_types::ErrorDetails;
383
546
///
384
547
/// let mut err_details = ErrorDetails::new();
385
548
///
@@ -407,7 +570,7 @@ impl ErrorDetails {
407
570
/// # Examples
408
571
///
409
572
/// ```
410
- /// use tonic_types::{ ErrorDetails} ;
573
+ /// use tonic_types::ErrorDetails;
411
574
///
412
575
/// let mut err_details = ErrorDetails::with_bad_request(vec![]);
413
576
///
0 commit comments