tonic_error/
lib.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::Display;
3use thiserror::Error;
4use tonic::metadata::errors::ToStrError;
5
6pub use tonic_error_impl::*;
7
8/// A derive trait to implement the `TryFrom` and `From` traits to and from
9/// `tonic::Status` for a custom error type.
10pub trait TonicError<'de>: Serialize + Deserialize<'de> + Display {}
11
12/// This is the error type for the `TryFrom` impl for `tonic::Status`. If the
13/// invalid `tonic::Code` is set in the `tonic::Status`; or if the metadata
14/// within the status is not set; or if invalid strings are used; or if the json
15/// cannot be parsed.  
16#[derive(Debug, Error)]
17pub enum ParseError {
18    #[error("missing metadata entry")]
19    MissingMetadata,
20
21    #[error("invalid status code set")]
22    InvalidStatusCode(tonic::Status),
23
24    #[error("could not parse metadata to string")]
25    MetadataParseError(#[from] ToStrError),
26
27    #[error("serde json error")]
28    JsonError(#[from] serde_json::Error),
29}