Skip to content

Commit 2cf3eea

Browse files
committed
Place StatusExt and RpcStatusExt into separate files.
Also does some minor module imports cleanup. The goal was to make room for a `CodeExt` trait.
1 parent eeb3268 commit 2cf3eea

18 files changed

+1102
-1109
lines changed

tonic-types/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,7 @@ pub mod pb {
182182
pub use pb::Status;
183183

184184
mod richer_error;
185-
186-
pub use richer_error::{
187-
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation, Help, HelpLink,
188-
LocalizedMessage, PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation,
189-
RequestInfo, ResourceInfo, RetryInfo, RpcStatusExt, StatusExt,
190-
};
185+
pub use richer_error::*;
191186

192187
mod sealed {
193188
pub trait Sealed {}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use std::{collections::HashMap, time};
1+
pub(super) mod vec;
22

3-
use super::std_messages::{
4-
BadRequest, DebugInfo, ErrorInfo, FieldViolation, Help, HelpLink, LocalizedMessage,
5-
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo,
6-
ResourceInfo, RetryInfo,
7-
};
3+
use std::{collections::HashMap, time};
84

9-
pub(crate) mod vec;
5+
use super::std_messages::*;
106

117
/// Groups the standard error messages structs. Provides associated
128
/// functions and methods to setup and edit each error message independently.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use super::super::std_messages::{
2-
BadRequest, DebugInfo, ErrorInfo, Help, LocalizedMessage, PreconditionFailure, QuotaFailure,
3-
RequestInfo, ResourceInfo, RetryInfo,
4-
};
1+
use super::super::std_messages::*;
52

63
/// Wraps the structs corresponding to the standard error messages, allowing
74
/// the implementation and handling of vectors containing any of them.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use prost::{
2+
bytes::{Bytes, BytesMut},
3+
DecodeError, Message,
4+
};
5+
use prost_types::Any;
6+
use tonic::Code;
7+
8+
use crate::pb;
9+
10+
pub(super) trait IntoAny {
11+
fn into_any(self) -> Any;
12+
}
13+
14+
pub(super) trait FromAny {
15+
fn from_any(any: Any) -> Result<Self, DecodeError>
16+
where
17+
Self: Sized;
18+
}
19+
20+
pub(super) trait FromAnyRef {
21+
fn from_any_ref(any: &Any) -> Result<Self, DecodeError>
22+
where
23+
Self: Sized;
24+
}
25+
26+
pub(super) fn gen_details_bytes(code: Code, message: &str, details: Vec<Any>) -> Bytes {
27+
let status = pb::Status {
28+
code: code as i32,
29+
message: message.to_owned(),
30+
details,
31+
};
32+
33+
let mut buf = BytesMut::with_capacity(status.encoded_len());
34+
35+
// Should never panic since `buf` is initialized with sufficient capacity
36+
status.encode(&mut buf).unwrap();
37+
38+
buf.freeze()
39+
}

0 commit comments

Comments
 (0)