1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(TonicError)]
pub fn tonic_error_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_tonic_error(&ast)
}
fn impl_tonic_error(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl<'t> TonicError<'t> for #name {}
use std::convert::TryFrom;
static CUSTOM_ERROR: &str = "x-custom-tonic-error";
impl TryFrom<tonic::Status> for #name {
type Error = anyhow::Error;
fn try_from(s: tonic::Status) -> Result<#name, Self::Error> {
match s.code() {
tonic::Code::Internal => {
if let Some(err) = s.metadata().get(CUSTOM_ERROR) {
Ok(serde_json::from_str(err.to_str()?)?)
} else {
Err(anyhow::anyhow!("missing metadata entry for key: {}", CUSTOM_ERROR))
}
}
c => Err(anyhow::anyhow!("not an internal error: {}", c)),
}
}
}
impl From<#name> for tonic::Status {
fn from(e: #name) -> Self {
let mut status = tonic::Status::internal(format!("internal error: {}", e));
status.metadata_mut().insert(CUSTOM_ERROR,
serde_json::to_string(&e)
.unwrap_or("could not serialize: {e}".to_string())
.parse()
.unwrap_or(tonic::metadata::MetadataValue::from_static("unable to create metadata value")));
status
}
}
};
gen.into()
}