tonic_error_impl/
lib.rs

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