Skip to content

Commit d54d02d

Browse files
authoredFeb 27, 2023
feat(types): Add gRPC Richer Error Model support (LocalizedMessage) (#1295)
* types: add support for `LocalizedMessage` error message type Following implementation at flemosr/tonic-richer-error. * types: add `ErrorDetails::with_help_link`
1 parent 838d91a commit d54d02d

File tree

6 files changed

+259
-11
lines changed

6 files changed

+259
-11
lines changed
 

‎tonic-types/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ mod richer_error;
4747

4848
pub use richer_error::{
4949
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation, Help, HelpLink,
50-
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo,
51-
ResourceInfo, RetryInfo, StatusExt,
50+
LocalizedMessage, PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation,
51+
RequestInfo, ResourceInfo, RetryInfo, StatusExt,
5252
};
5353

5454
mod sealed {

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

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::{collections::HashMap, time};
22

33
use super::std_messages::{
4-
BadRequest, DebugInfo, ErrorInfo, FieldViolation, Help, HelpLink, PreconditionFailure,
5-
PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo, ResourceInfo, RetryInfo,
4+
BadRequest, DebugInfo, ErrorInfo, FieldViolation, Help, HelpLink, LocalizedMessage,
5+
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo,
6+
ResourceInfo, RetryInfo,
67
};
78

89
pub(crate) mod vec;
@@ -40,6 +41,9 @@ pub struct ErrorDetails {
4041

4142
/// This field stores [`Help`] data, if any.
4243
pub(crate) help: Option<Help>,
44+
45+
/// This field stores [`LocalizedMessage`] data, if any.
46+
pub(crate) localized_message: Option<LocalizedMessage>,
4347
}
4448

4549
impl ErrorDetails {
@@ -337,6 +341,46 @@ impl ErrorDetails {
337341
}
338342
}
339343

344+
/// Generates an [`ErrorDetails`] struct with [`Help`] details (one
345+
/// [`HelpLink`] set) and remaining fields set to `None`.
346+
///
347+
/// # Examples
348+
///
349+
/// ```
350+
/// use tonic_types::ErrorDetails;
351+
///
352+
/// let err_details = ErrorDetails::with_help_link(
353+
/// "description of link a",
354+
/// "resource-a.example.local"
355+
/// );
356+
/// ```
357+
pub fn with_help_link(description: impl Into<String>, url: impl Into<String>) -> Self {
358+
ErrorDetails {
359+
help: Some(Help::with_link(description, url)),
360+
..ErrorDetails::new()
361+
}
362+
}
363+
364+
/// Generates an [`ErrorDetails`] struct with [`LocalizedMessage`] details
365+
/// and remaining fields set to `None`.
366+
///
367+
/// # Examples
368+
///
369+
/// ```
370+
/// use tonic_types::ErrorDetails;
371+
///
372+
/// let err_details = ErrorDetails::with_localized_message(
373+
/// "en-US",
374+
/// "message for the user"
375+
/// );
376+
/// ```
377+
pub fn with_localized_message(locale: impl Into<String>, message: impl Into<String>) -> Self {
378+
ErrorDetails {
379+
localized_message: Some(LocalizedMessage::new(locale, message)),
380+
..ErrorDetails::new()
381+
}
382+
}
383+
340384
/// Get [`RetryInfo`] details, if any.
341385
pub fn retry_info(&self) -> Option<RetryInfo> {
342386
self.retry_info.clone()
@@ -382,6 +426,11 @@ impl ErrorDetails {
382426
self.help.clone()
383427
}
384428

429+
/// Get [`LocalizedMessage`] details, if any.
430+
pub fn localized_message(&self) -> Option<LocalizedMessage> {
431+
self.localized_message.clone()
432+
}
433+
385434
/// Set [`RetryInfo`] details. Can be chained with other `.set_` and
386435
/// `.add_` [`ErrorDetails`] methods.
387436
///
@@ -809,4 +858,25 @@ impl ErrorDetails {
809858
}
810859
false
811860
}
861+
862+
/// Set [`LocalizedMessage`] details. Can be chained with other `.set_` and
863+
/// `.add_` [`ErrorDetails`] methods.
864+
///
865+
/// # Examples
866+
///
867+
/// ```
868+
/// use tonic_types::ErrorDetails;
869+
///
870+
/// let mut err_details = ErrorDetails::new();
871+
///
872+
/// err_details.set_localized_message("en-US", "message for the user");
873+
/// ```
874+
pub fn set_localized_message(
875+
&mut self,
876+
locale: impl Into<String>,
877+
message: impl Into<String>,
878+
) -> &mut Self {
879+
self.localized_message = Some(LocalizedMessage::new(locale, message));
880+
self
881+
}
812882
}

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::super::std_messages::{
2-
BadRequest, DebugInfo, ErrorInfo, Help, PreconditionFailure, QuotaFailure, RequestInfo,
3-
ResourceInfo, RetryInfo,
2+
BadRequest, DebugInfo, ErrorInfo, Help, LocalizedMessage, PreconditionFailure, QuotaFailure,
3+
RequestInfo, ResourceInfo, RetryInfo,
44
};
55

66
/// Wraps the structs corresponding to the standard error messages, allowing
@@ -34,6 +34,9 @@ pub enum ErrorDetail {
3434

3535
/// Wraps the [`Help`] struct.
3636
Help(Help),
37+
38+
/// Wraps the [`LocalizedMessage`] struct.
39+
LocalizedMessage(LocalizedMessage),
3740
}
3841

3942
impl From<RetryInfo> for ErrorDetail {
@@ -89,3 +92,9 @@ impl From<Help> for ErrorDetail {
8992
ErrorDetail::Help(err_detail)
9093
}
9194
}
95+
96+
impl From<LocalizedMessage> for ErrorDetail {
97+
fn from(err_detail: LocalizedMessage) -> Self {
98+
ErrorDetail::LocalizedMessage(err_detail)
99+
}
100+
}

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

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

1313
pub use error_details::{vec::ErrorDetail, ErrorDetails};
1414
pub use std_messages::{
15-
BadRequest, DebugInfo, ErrorInfo, FieldViolation, Help, HelpLink, PreconditionFailure,
16-
PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo, ResourceInfo, RetryInfo,
15+
BadRequest, DebugInfo, ErrorInfo, FieldViolation, Help, HelpLink, LocalizedMessage,
16+
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo,
17+
ResourceInfo, RetryInfo,
1718
};
1819

1920
trait IntoAny {
@@ -446,6 +447,28 @@ pub trait StatusExt: crate::sealed::Sealed {
446447
/// }
447448
/// ```
448449
fn get_details_help(&self) -> Option<Help>;
450+
451+
/// Get first [`LocalizedMessage`] details found on `tonic::Status`, if
452+
/// any. If some `prost::DecodeError` occurs, returns `None`.
453+
///
454+
/// # Examples
455+
///
456+
/// ```
457+
/// use tonic::{Status, Response};
458+
/// use tonic_types::StatusExt;
459+
///
460+
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
461+
/// match req_result {
462+
/// Ok(_) => {},
463+
/// Err(status) => {
464+
/// if let Some(localized_message) = status.get_details_localized_message() {
465+
/// // Handle localized_message details
466+
/// }
467+
/// }
468+
/// };
469+
/// }
470+
/// ```
471+
fn get_details_localized_message(&self) -> Option<LocalizedMessage>;
449472
}
450473

451474
impl crate::sealed::Sealed for tonic::Status {}
@@ -497,6 +520,10 @@ impl StatusExt for tonic::Status {
497520
conv_details.push(help.into_any());
498521
}
499522

523+
if let Some(localized_message) = details.localized_message {
524+
conv_details.push(localized_message.into_any());
525+
}
526+
500527
let details = gen_details_bytes(code, &message, conv_details);
501528

502529
tonic::Status::with_details_and_metadata(code, message, details, metadata)
@@ -545,6 +572,9 @@ impl StatusExt for tonic::Status {
545572
ErrorDetail::Help(help) => {
546573
conv_details.push(help.into_any());
547574
}
575+
ErrorDetail::LocalizedMessage(loc_message) => {
576+
conv_details.push(loc_message.into_any());
577+
}
548578
}
549579
}
550580

@@ -600,6 +630,9 @@ impl StatusExt for tonic::Status {
600630
Help::TYPE_URL => {
601631
details.help = Some(Help::from_any(any)?);
602632
}
633+
LocalizedMessage::TYPE_URL => {
634+
details.localized_message = Some(LocalizedMessage::from_any(any)?);
635+
}
603636
_ => {}
604637
}
605638
}
@@ -645,6 +678,9 @@ impl StatusExt for tonic::Status {
645678
Help::TYPE_URL => {
646679
details.push(Help::from_any(any)?.into());
647680
}
681+
LocalizedMessage::TYPE_URL => {
682+
details.push(LocalizedMessage::from_any(any)?.into());
683+
}
648684
_ => {}
649685
}
650686
}
@@ -781,6 +817,20 @@ impl StatusExt for tonic::Status {
781817

782818
None
783819
}
820+
821+
fn get_details_localized_message(&self) -> Option<LocalizedMessage> {
822+
let status = pb::Status::decode(self.details()).ok()?;
823+
824+
for any in status.details.into_iter() {
825+
if any.type_url.as_str() == LocalizedMessage::TYPE_URL {
826+
if let Ok(detail) = LocalizedMessage::from_any(any) {
827+
return Some(detail);
828+
}
829+
}
830+
}
831+
832+
None
833+
}
784834
}
785835

786836
#[cfg(test)]
@@ -789,8 +839,8 @@ mod tests {
789839
use tonic::{Code, Status};
790840

791841
use super::{
792-
BadRequest, DebugInfo, ErrorDetails, ErrorInfo, Help, PreconditionFailure, QuotaFailure,
793-
RequestInfo, ResourceInfo, RetryInfo, StatusExt,
842+
BadRequest, DebugInfo, ErrorDetails, ErrorInfo, Help, LocalizedMessage,
843+
PreconditionFailure, QuotaFailure, RequestInfo, ResourceInfo, RetryInfo, StatusExt,
794844
};
795845

796846
#[test]
@@ -812,7 +862,8 @@ mod tests {
812862
.add_bad_request_violation("field", "description")
813863
.set_request_info("request-id", "some-request-data")
814864
.set_resource_info("resource-type", "resource-name", "owner", "description")
815-
.add_help_link("link to resource", "resource.example.local");
865+
.add_help_link("link to resource", "resource.example.local")
866+
.set_localized_message("en-US", "message for the user");
816867

817868
let fmt_details = format!("{:?}", err_details);
818869

@@ -830,6 +881,7 @@ mod tests {
830881
RequestInfo::new("request-id", "some-request-data").into(),
831882
ResourceInfo::new("resource-type", "resource-name", "owner", "description").into(),
832883
Help::with_link("link to resource", "resource.example.local").into(),
884+
LocalizedMessage::new("en-US", "message for the user").into(),
833885
];
834886

835887
let fmt_details_vec = format!("{:?}", err_details_vec);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use prost::{DecodeError, Message};
2+
use prost_types::Any;
3+
4+
use super::super::{pb, FromAny, IntoAny};
5+
6+
/// Used to encode/decode the `LocalizedMessage` standard error message
7+
/// described in [error_details.proto]. Provides a localized error message
8+
/// that is safe to return to the user.
9+
///
10+
/// [error_details.proto]: https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto
11+
#[derive(Clone, Debug)]
12+
pub struct LocalizedMessage {
13+
/// Locale used, following the specification defined in [BCP 47]. For
14+
/// example: "en-US", "fr-CH" or "es-MX".
15+
///
16+
/// [BCP 47]: http://www.rfc-editor.org/rfc/bcp/bcp47.txt
17+
pub locale: String,
18+
19+
/// Message corresponding to the locale.
20+
pub message: String,
21+
}
22+
23+
impl LocalizedMessage {
24+
/// Type URL of the `LocalizedMessage` standard error message type.
25+
pub const TYPE_URL: &'static str = "type.googleapis.com/google.rpc.LocalizedMessage";
26+
27+
/// Creates a new [`LocalizedMessage`] struct.
28+
pub fn new(locale: impl Into<String>, message: impl Into<String>) -> Self {
29+
LocalizedMessage {
30+
locale: locale.into(),
31+
message: message.into(),
32+
}
33+
}
34+
35+
/// Returns `true` if [`LocalizedMessage`] fields are empty, and `false` if
36+
/// they are not.
37+
pub fn is_empty(&self) -> bool {
38+
self.locale.is_empty() && self.message.is_empty()
39+
}
40+
}
41+
42+
impl IntoAny for LocalizedMessage {
43+
fn into_any(self) -> Any {
44+
let detail_data = pb::LocalizedMessage {
45+
locale: self.locale,
46+
message: self.message,
47+
};
48+
49+
Any {
50+
type_url: LocalizedMessage::TYPE_URL.to_string(),
51+
value: detail_data.encode_to_vec(),
52+
}
53+
}
54+
}
55+
56+
impl FromAny for LocalizedMessage {
57+
fn from_any(any: Any) -> Result<Self, DecodeError> {
58+
let buf: &[u8] = &any.value;
59+
let loc_message = pb::LocalizedMessage::decode(buf)?;
60+
61+
let loc_message = LocalizedMessage {
62+
locale: loc_message.locale,
63+
message: loc_message.message,
64+
};
65+
66+
Ok(loc_message)
67+
}
68+
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::super::super::{FromAny, IntoAny};
73+
use super::LocalizedMessage;
74+
75+
#[test]
76+
fn gen_localized_message() {
77+
let loc_message = LocalizedMessage::new("en-US", "message for the user");
78+
79+
let formatted = format!("{:?}", loc_message);
80+
81+
let expected_filled =
82+
"LocalizedMessage { locale: \"en-US\", message: \"message for the user\" }";
83+
84+
assert!(
85+
formatted.eq(expected_filled),
86+
"filled LocalizedMessage differs from expected result"
87+
);
88+
89+
let gen_any = loc_message.into_any();
90+
91+
let formatted = format!("{:?}", gen_any);
92+
93+
let expected =
94+
"Any { type_url: \"type.googleapis.com/google.rpc.LocalizedMessage\", value: [10, 5, 101, 110, 45, 85, 83, 18, 20, 109, 101, 115, 115, 97, 103, 101, 32, 102, 111, 114, 32, 116, 104, 101, 32, 117, 115, 101, 114] }";
95+
96+
assert!(
97+
formatted.eq(expected),
98+
"Any from filled LocalizedMessage differs from expected result"
99+
);
100+
101+
let br_details = match LocalizedMessage::from_any(gen_any) {
102+
Err(error) => panic!("Error generating LocalizedMessage from Any: {:?}", error),
103+
Ok(from_any) => from_any,
104+
};
105+
106+
let formatted = format!("{:?}", br_details);
107+
108+
assert!(
109+
formatted.eq(expected_filled),
110+
"LocalizedMessage from Any differs from expected result"
111+
);
112+
}
113+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ pub use resource_info::ResourceInfo;
3333
mod help;
3434

3535
pub use help::{Help, HelpLink};
36+
37+
mod loc_message;
38+
39+
pub use loc_message::LocalizedMessage;

0 commit comments

Comments
 (0)
Please sign in to comment.