Skip to content

Commit

Permalink
Place StatusExt and RpcStatusExt into separate files.
Browse files Browse the repository at this point in the history
Also does some minor module imports cleanup.

The goal is to make room for a `CodeExt` trait.
  • Loading branch information
gibbz00 committed Mar 16, 2024
1 parent eeb3268 commit c44ab3d
Show file tree
Hide file tree
Showing 18 changed files with 1,103 additions and 1,109 deletions.
7 changes: 1 addition & 6 deletions tonic-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ pub mod pb {
pub use pb::Status;

mod richer_error;

pub use richer_error::{
BadRequest, DebugInfo, ErrorDetail, ErrorDetails, ErrorInfo, FieldViolation, Help, HelpLink,
LocalizedMessage, PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation,
RequestInfo, ResourceInfo, RetryInfo, RpcStatusExt, StatusExt,
};
pub use richer_error::*;

mod sealed {
pub trait Sealed {}
Expand Down
10 changes: 3 additions & 7 deletions tonic-types/src/richer_error/error_details/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::{collections::HashMap, time};
pub(super) mod vec;

use super::std_messages::{
BadRequest, DebugInfo, ErrorInfo, FieldViolation, Help, HelpLink, LocalizedMessage,
PreconditionFailure, PreconditionViolation, QuotaFailure, QuotaViolation, RequestInfo,
ResourceInfo, RetryInfo,
};
use std::{collections::HashMap, time};

pub(crate) mod vec;
use super::std_messages::*;

/// Groups the standard error messages structs. Provides associated
/// functions and methods to setup and edit each error message independently.
Expand Down
5 changes: 1 addition & 4 deletions tonic-types/src/richer_error/error_details/vec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use super::super::std_messages::{
BadRequest, DebugInfo, ErrorInfo, Help, LocalizedMessage, PreconditionFailure, QuotaFailure,
RequestInfo, ResourceInfo, RetryInfo,
};
use super::super::std_messages::*;

/// Wraps the structs corresponding to the standard error messages, allowing
/// the implementation and handling of vectors containing any of them.
Expand Down
40 changes: 40 additions & 0 deletions tonic-types/src/richer_error/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use prost::{
bytes::{Bytes, BytesMut},
DecodeError, Message,
};
use prost_types::Any;
use tonic::Code;

use crate::pb;

pub(super) trait IntoAny {
fn into_any(self) -> Any;
}

#[allow(dead_code)]
pub(super) trait FromAny {
fn from_any(any: Any) -> Result<Self, DecodeError>
where
Self: Sized;
}

pub(super) trait FromAnyRef {
fn from_any_ref(any: &Any) -> Result<Self, DecodeError>
where
Self: Sized;
}

pub(super) fn gen_details_bytes(code: Code, message: &str, details: Vec<Any>) -> Bytes {
let status = pb::Status {
code: code as i32,
message: message.to_owned(),
details,
};

let mut buf = BytesMut::with_capacity(status.encoded_len());

// Should never panic since `buf` is initialized with sufficient capacity
status.encode(&mut buf).unwrap();

buf.freeze()
}

0 comments on commit c44ab3d

Please sign in to comment.