From 537fa573465fa31d369506c9d98b4f48ffc8f895 Mon Sep 17 00:00:00 2001 From: Cameron Dart Date: Sat, 28 Mar 2020 20:48:06 -0700 Subject: [PATCH 1/3] Give an option for creating errors with binary Details field --- tonic/src/status.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 1d9452b12..d37c44145 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -1,6 +1,7 @@ use bytes::Bytes; use http::header::{HeaderMap, HeaderValue}; use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS}; +use prost::Message; use std::{borrow::Cow, error::Error, fmt}; use tracing::{debug, trace, warn}; @@ -401,6 +402,25 @@ impl Status { Ok(()) } + + /// Create a new `Status` with the associated code, message, and binary details field. + pub fn with_raw_details(code: Code, message: impl Into, details: Bytes) -> Status { + Status { + code, + message: message.into(), + details: details, + } + } + + /// Create a new `Status` with the associated code, message, and protobuf details field. + pub fn with_details(code: Code, message: impl Into, details: impl Message) -> Status { + let mut bytes = vec![]; + details + .encode(&mut bytes) + .expect("Message only errors if not enough space"); + Status::with_raw_details(code, message, bytes.into()) + } + } impl fmt::Debug for Status { From c347c88831067cae8407988ff39af8543aad2dfd Mon Sep 17 00:00:00 2001 From: Cameron Dart Date: Sat, 28 Mar 2020 21:36:45 -0700 Subject: [PATCH 2/3] squashme rustfmt --- tonic/src/status.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index d37c44145..9f0b4c82f 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -420,7 +420,6 @@ impl Status { .expect("Message only errors if not enough space"); Status::with_raw_details(code, message, bytes.into()) } - } impl fmt::Debug for Status { From f6e71d5d16e2f436f6d2da937d2c2308567fbd42 Mon Sep 17 00:00:00 2001 From: Cameron Dart Date: Sun, 29 Mar 2020 10:54:27 -0700 Subject: [PATCH 3/3] Remove prost dependency and only expose with_details constructor that takes bytes --- tonic/src/status.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 9f0b4c82f..90ea9eef6 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -1,7 +1,6 @@ use bytes::Bytes; use http::header::{HeaderMap, HeaderValue}; use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS}; -use prost::Message; use std::{borrow::Cow, error::Error, fmt}; use tracing::{debug, trace, warn}; @@ -404,22 +403,13 @@ impl Status { } /// Create a new `Status` with the associated code, message, and binary details field. - pub fn with_raw_details(code: Code, message: impl Into, details: Bytes) -> Status { + pub fn with_details(code: Code, message: impl Into, details: Bytes) -> Status { Status { code, message: message.into(), details: details, } } - - /// Create a new `Status` with the associated code, message, and protobuf details field. - pub fn with_details(code: Code, message: impl Into, details: impl Message) -> Status { - let mut bytes = vec![]; - details - .encode(&mut bytes) - .expect("Message only errors if not enough space"); - Status::with_raw_details(code, message, bytes.into()) - } } impl fmt::Debug for Status {