1
1
use std:: { collections:: HashMap , time} ;
2
2
3
3
use super :: std_messages:: {
4
- BadRequest , DebugInfo , ErrorInfo , FieldViolation , PreconditionFailure , PreconditionViolation ,
5
- QuotaFailure , QuotaViolation , RequestInfo , ResourceInfo , RetryInfo ,
4
+ BadRequest , DebugInfo , ErrorInfo , FieldViolation , Help , HelpLink , PreconditionFailure ,
5
+ PreconditionViolation , QuotaFailure , QuotaViolation , RequestInfo , ResourceInfo , RetryInfo ,
6
6
} ;
7
7
8
8
pub ( crate ) mod vec;
@@ -37,6 +37,9 @@ pub struct ErrorDetails {
37
37
38
38
/// This field stores [`ResourceInfo`] data, if any.
39
39
pub ( crate ) resource_info : Option < ResourceInfo > ,
40
+
41
+ /// This field stores [`Help`] data, if any.
42
+ pub ( crate ) help : Option < Help > ,
40
43
}
41
44
42
45
impl ErrorDetails {
@@ -83,7 +86,10 @@ impl ErrorDetails {
83
86
///
84
87
/// let err_details = ErrorDetails::with_debug_info(err_stack, "error details");
85
88
/// ```
86
- pub fn with_debug_info ( stack_entries : Vec < String > , detail : impl Into < String > ) -> Self {
89
+ pub fn with_debug_info (
90
+ stack_entries : impl Into < Vec < String > > ,
91
+ detail : impl Into < String > ,
92
+ ) -> Self {
87
93
ErrorDetails {
88
94
debug_info : Some ( DebugInfo :: new ( stack_entries, detail) ) ,
89
95
..ErrorDetails :: new ( )
@@ -103,7 +109,7 @@ impl ErrorDetails {
103
109
/// QuotaViolation::new("subject 2", "description 2"),
104
110
/// ]);
105
111
/// ```
106
- pub fn with_quota_failure ( violations : Vec < QuotaViolation > ) -> Self {
112
+ pub fn with_quota_failure ( violations : impl Into < Vec < QuotaViolation > > ) -> Self {
107
113
ErrorDetails {
108
114
quota_failure : Some ( QuotaFailure :: new ( violations) ) ,
109
115
..ErrorDetails :: new ( )
@@ -176,7 +182,7 @@ impl ErrorDetails {
176
182
/// ),
177
183
/// ]);
178
184
/// ```
179
- pub fn with_precondition_failure ( violations : Vec < PreconditionViolation > ) -> Self {
185
+ pub fn with_precondition_failure ( violations : impl Into < Vec < PreconditionViolation > > ) -> Self {
180
186
ErrorDetails {
181
187
precondition_failure : Some ( PreconditionFailure :: new ( violations) ) ,
182
188
..ErrorDetails :: new ( )
@@ -226,7 +232,7 @@ impl ErrorDetails {
226
232
/// FieldViolation::new("field_2", "description 2"),
227
233
/// ]);
228
234
/// ```
229
- pub fn with_bad_request ( field_violations : Vec < FieldViolation > ) -> Self {
235
+ pub fn with_bad_request ( field_violations : impl Into < Vec < FieldViolation > > ) -> Self {
230
236
ErrorDetails {
231
237
bad_request : Some ( BadRequest :: new ( field_violations) ) ,
232
238
..ErrorDetails :: new ( )
@@ -311,6 +317,26 @@ impl ErrorDetails {
311
317
}
312
318
}
313
319
320
+ /// Generates an [`ErrorDetails`] struct with [`Help`] details and
321
+ /// remaining fields set to `None`.
322
+ ///
323
+ /// # Examples
324
+ ///
325
+ /// ```
326
+ /// use tonic_types::{ErrorDetails, HelpLink};
327
+ ///
328
+ /// let err_details = ErrorDetails::with_help(vec![
329
+ /// HelpLink::new("description of link a", "resource-a.example.local"),
330
+ /// HelpLink::new("description of link b", "resource-b.example.local"),
331
+ /// ]);
332
+ /// ```
333
+ pub fn with_help ( links : impl Into < Vec < HelpLink > > ) -> Self {
334
+ ErrorDetails {
335
+ help : Some ( Help :: new ( links) ) ,
336
+ ..ErrorDetails :: new ( )
337
+ }
338
+ }
339
+
314
340
/// Get [`RetryInfo`] details, if any.
315
341
pub fn retry_info ( & self ) -> Option < RetryInfo > {
316
342
self . retry_info . clone ( )
@@ -351,6 +377,11 @@ impl ErrorDetails {
351
377
self . resource_info . clone ( )
352
378
}
353
379
380
+ /// Get [`Help`] details, if any.
381
+ pub fn help ( & self ) -> Option < Help > {
382
+ self . help . clone ( )
383
+ }
384
+
354
385
/// Set [`RetryInfo`] details. Can be chained with other `.set_` and
355
386
/// `.add_` [`ErrorDetails`] methods.
356
387
///
@@ -385,7 +416,7 @@ impl ErrorDetails {
385
416
/// ```
386
417
pub fn set_debug_info (
387
418
& mut self ,
388
- stack_entries : Vec < String > ,
419
+ stack_entries : impl Into < Vec < String > > ,
389
420
detail : impl Into < String > ,
390
421
) -> & mut Self {
391
422
self . debug_info = Some ( DebugInfo :: new ( stack_entries, detail) ) ;
@@ -407,7 +438,7 @@ impl ErrorDetails {
407
438
/// QuotaViolation::new("subject 2", "description 2"),
408
439
/// ]);
409
440
/// ```
410
- pub fn set_quota_failure ( & mut self , violations : Vec < QuotaViolation > ) -> & mut Self {
441
+ pub fn set_quota_failure ( & mut self , violations : impl Into < Vec < QuotaViolation > > ) -> & mut Self {
411
442
self . quota_failure = Some ( QuotaFailure :: new ( violations) ) ;
412
443
self
413
444
}
@@ -515,7 +546,7 @@ impl ErrorDetails {
515
546
/// ```
516
547
pub fn set_precondition_failure (
517
548
& mut self ,
518
- violations : Vec < PreconditionViolation > ,
549
+ violations : impl Into < Vec < PreconditionViolation > > ,
519
550
) -> & mut Self {
520
551
self . precondition_failure = Some ( PreconditionFailure :: new ( violations) ) ;
521
552
self
@@ -601,7 +632,7 @@ impl ErrorDetails {
601
632
/// FieldViolation::new("field_2", "description 2"),
602
633
/// ]);
603
634
/// ```
604
- pub fn set_bad_request ( & mut self , violations : Vec < FieldViolation > ) -> & mut Self {
635
+ pub fn set_bad_request ( & mut self , violations : impl Into < Vec < FieldViolation > > ) -> & mut Self {
605
636
self . bad_request = Some ( BadRequest :: new ( violations) ) ;
606
637
self
607
638
}
@@ -706,4 +737,76 @@ impl ErrorDetails {
706
737
) ) ;
707
738
self
708
739
}
740
+
741
+ /// Set [`Help`] details. Can be chained with other `.set_` and `.add_`
742
+ /// [`ErrorDetails`] methods.
743
+ ///
744
+ /// # Examples
745
+ ///
746
+ /// ```
747
+ /// use tonic_types::{ErrorDetails, HelpLink};
748
+ ///
749
+ /// let mut err_details = ErrorDetails::new();
750
+ ///
751
+ /// err_details.set_help(vec![
752
+ /// HelpLink::new("description of link a", "resource-a.example.local"),
753
+ /// HelpLink::new("description of link b", "resource-b.example.local"),
754
+ /// ]);
755
+ /// ```
756
+ pub fn set_help ( & mut self , links : impl Into < Vec < HelpLink > > ) -> & mut Self {
757
+ self . help = Some ( Help :: new ( links) ) ;
758
+ self
759
+ }
760
+
761
+ /// Adds a [`HelpLink`] to [`Help`] details. Sets [`Help`] details if it is
762
+ /// not set yet. Can be chained with other `.set_` and `.add_`
763
+ /// [`ErrorDetails`] methods.
764
+ ///
765
+ /// # Examples
766
+ ///
767
+ /// ```
768
+ /// use tonic_types::ErrorDetails;
769
+ ///
770
+ /// let mut err_details = ErrorDetails::new();
771
+ ///
772
+ /// err_details.add_help_link("description of link", "resource.example.local");
773
+ /// ```
774
+ pub fn add_help_link (
775
+ & mut self ,
776
+ description : impl Into < String > ,
777
+ url : impl Into < String > ,
778
+ ) -> & mut Self {
779
+ match & mut self . help {
780
+ Some ( help) => {
781
+ help. add_link ( description, url) ;
782
+ }
783
+ None => {
784
+ self . help = Some ( Help :: with_link ( description, url) ) ;
785
+ }
786
+ } ;
787
+ self
788
+ }
789
+
790
+ /// Returns `true` if [`Help`] is set and its `links` vector is not empty,
791
+ /// otherwise returns `false`.
792
+ ///
793
+ /// # Examples
794
+ ///
795
+ /// ```
796
+ /// use tonic_types::ErrorDetails;
797
+ ///
798
+ /// let mut err_details = ErrorDetails::with_help(vec![]);
799
+ ///
800
+ /// assert_eq!(err_details.has_help_links(), false);
801
+ ///
802
+ /// err_details.add_help_link("description of link", "resource.example.local");
803
+ ///
804
+ /// assert_eq!(err_details.has_help_links(), true);
805
+ /// ```
806
+ pub fn has_help_links ( & self ) -> bool {
807
+ if let Some ( help) = & self . help {
808
+ return !help. links . is_empty ( ) ;
809
+ }
810
+ false
811
+ }
709
812
}
0 commit comments