@@ -13,7 +13,7 @@ use super::pb;
13
13
pub use error_details:: { vec:: ErrorDetail , ErrorDetails } ;
14
14
pub use std_messages:: {
15
15
BadRequest , DebugInfo , ErrorInfo , FieldViolation , PreconditionFailure , PreconditionViolation ,
16
- QuotaFailure , QuotaViolation , RequestInfo , RetryInfo ,
16
+ QuotaFailure , QuotaViolation , RequestInfo , ResourceInfo , RetryInfo ,
17
17
} ;
18
18
19
19
trait IntoAny {
@@ -153,12 +153,13 @@ pub trait StatusExt: crate::sealed::Sealed {
153
153
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
154
154
/// match req_result {
155
155
/// Ok(_) => {},
156
- /// Err(status) => {
157
- /// let err_details = status.get_error_details();
158
- /// if let Some(bad_request) = err_details.bad_request() {
159
- /// // Handle bad_request details
156
+ /// Err(status) => match status.check_error_details() {
157
+ /// Ok(err_details) => {
158
+ /// // Handle extracted details
159
+ /// }
160
+ /// Err(decode_error) => {
161
+ /// // Handle decode_error
160
162
/// }
161
- /// // ...
162
163
/// }
163
164
/// };
164
165
/// }
@@ -201,19 +202,17 @@ pub trait StatusExt: crate::sealed::Sealed {
201
202
///
202
203
/// ```
203
204
/// use tonic::{Status, Response};
204
- /// use tonic_types::{ErrorDetail, StatusExt} ;
205
+ /// use tonic_types::StatusExt;
205
206
///
206
207
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
207
208
/// match req_result {
208
209
/// Ok(_) => {},
209
- /// Err(status) => {
210
- /// match status.check_error_details_vec() {
211
- /// Ok(err_details) => {
212
- /// // Handle extracted details
213
- /// }
214
- /// Err(decode_error) => {
215
- /// // Handle decode_error
216
- /// }
210
+ /// Err(status) => match status.check_error_details_vec() {
211
+ /// Ok(err_details) => {
212
+ /// // Handle extracted details
213
+ /// }
214
+ /// Err(decode_error) => {
215
+ /// // Handle decode_error
217
216
/// }
218
217
/// }
219
218
/// };
@@ -403,6 +402,28 @@ pub trait StatusExt: crate::sealed::Sealed {
403
402
/// }
404
403
/// ```
405
404
fn get_details_request_info ( & self ) -> Option < RequestInfo > ;
405
+
406
+ /// Get first [`ResourceInfo`] details found on `tonic::Status`, if any.
407
+ /// If some `prost::DecodeError` occurs, returns `None`.
408
+ ///
409
+ /// # Examples
410
+ ///
411
+ /// ```
412
+ /// use tonic::{Status, Response};
413
+ /// use tonic_types::StatusExt;
414
+ ///
415
+ /// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
416
+ /// match req_result {
417
+ /// Ok(_) => {},
418
+ /// Err(status) => {
419
+ /// if let Some(resource_info) = status.get_details_resource_info() {
420
+ /// // Handle resource_info details
421
+ /// }
422
+ /// }
423
+ /// };
424
+ /// }
425
+ /// ```
426
+ fn get_details_resource_info ( & self ) -> Option < ResourceInfo > ;
406
427
}
407
428
408
429
impl crate :: sealed:: Sealed for tonic:: Status { }
@@ -446,6 +467,10 @@ impl StatusExt for tonic::Status {
446
467
conv_details. push ( request_info. into_any ( ) ) ;
447
468
}
448
469
470
+ if let Some ( resource_info) = details. resource_info {
471
+ conv_details. push ( resource_info. into_any ( ) ) ;
472
+ }
473
+
449
474
let details = gen_details_bytes ( code, & message, conv_details) ;
450
475
451
476
tonic:: Status :: with_details_and_metadata ( code, message, details, metadata)
@@ -488,6 +513,9 @@ impl StatusExt for tonic::Status {
488
513
ErrorDetail :: RequestInfo ( req_info) => {
489
514
conv_details. push ( req_info. into_any ( ) ) ;
490
515
}
516
+ ErrorDetail :: ResourceInfo ( res_info) => {
517
+ conv_details. push ( res_info. into_any ( ) ) ;
518
+ }
491
519
}
492
520
}
493
521
@@ -537,6 +565,9 @@ impl StatusExt for tonic::Status {
537
565
RequestInfo :: TYPE_URL => {
538
566
details. request_info = Some ( RequestInfo :: from_any ( any) ?) ;
539
567
}
568
+ ResourceInfo :: TYPE_URL => {
569
+ details. resource_info = Some ( ResourceInfo :: from_any ( any) ?) ;
570
+ }
540
571
_ => { }
541
572
}
542
573
}
@@ -576,6 +607,9 @@ impl StatusExt for tonic::Status {
576
607
RequestInfo :: TYPE_URL => {
577
608
details. push ( RequestInfo :: from_any ( any) ?. into ( ) ) ;
578
609
}
610
+ ResourceInfo :: TYPE_URL => {
611
+ details. push ( ResourceInfo :: from_any ( any) ?. into ( ) ) ;
612
+ }
579
613
_ => { }
580
614
}
581
615
}
@@ -684,6 +718,20 @@ impl StatusExt for tonic::Status {
684
718
685
719
None
686
720
}
721
+
722
+ fn get_details_resource_info ( & self ) -> Option < ResourceInfo > {
723
+ let status = pb:: Status :: decode ( self . details ( ) ) . ok ( ) ?;
724
+
725
+ for any in status. details . into_iter ( ) {
726
+ if any. type_url . as_str ( ) == ResourceInfo :: TYPE_URL {
727
+ if let Ok ( detail) = ResourceInfo :: from_any ( any) {
728
+ return Some ( detail) ;
729
+ }
730
+ }
731
+ }
732
+
733
+ None
734
+ }
687
735
}
688
736
689
737
#[ cfg( test) ]
0 commit comments