wireman_core/
error.rs

1#![allow(clippy::module_name_repetitions, clippy::enum_variant_names)]
2use prost_reflect::DescriptorError;
3use thiserror::Error as ThisError;
4
5/// The result type for this library
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// The error type
9#[derive(ThisError, Debug)]
10pub enum Error {
11    /// Internal error
12    #[error("internal error {0}")]
13    Internal(String),
14
15    /// Failed to deserialize `DynamicMessage` from json
16    #[error("error deserializing message from json")]
17    DeserializeMessage(#[source] serde_json::Error),
18
19    /// Protox failed to compile the proto files
20    #[error("error compiling proto files")]
21    ProtoxCompileError(#[source] protox::Error),
22
23    /// Protox failed to compile the proto files
24    #[error("error generating the descriptor pool")]
25    DescriptorError(#[source] DescriptorError),
26
27    /// Failed to create a grpc channel
28    #[error("error creating grpc channel")]
29    GrpcChannelCreateError(#[source] protox::Error),
30
31    /// Grpc channel is not ready
32    #[error("grpc channel is not ready: {0}")]
33    GrpcNotReady(#[from] tonic::transport::Error),
34
35    /// Failed to make a unary grpc call
36    #[error("grpc: {0}")]
37    GrpcError(GrpcStatus),
38
39    /// Failed to load the custom TLS certificate
40    #[error("failed to load custom TLS certificate")]
41    LoadTLSCertificateError(#[source] std::io::Error),
42
43    /// Failed to serialize proto messages
44    #[error("failed to serialize proto message")]
45    SerializeJsonError(#[source] serde_json::Error),
46
47    /// Failed to serialize proto messages
48    #[error("failed to serialize the message")]
49    SerializeMessageError(String),
50
51    /// Failed to parse to ascii
52    #[error("error parsing to ascii")]
53    ParseToAsciiError,
54}
55
56impl From<tonic::Status> for Error {
57    fn from(status: tonic::Status) -> Self {
58        Self::GrpcError(status.into())
59    }
60}
61
62/// A status describing the result of a grpc call
63#[derive(Debug)]
64pub struct GrpcStatus {
65    /// The grpc status code
66    pub code: tonic::Code,
67    /// The error message
68    pub message: String,
69}
70
71impl std::fmt::Display for GrpcStatus {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        write!(f, "status: {:?}, message: {:?}", self.code, self.message)
74    }
75}
76
77impl From<tonic::Status> for GrpcStatus {
78    fn from(status: tonic::Status) -> Self {
79        GrpcStatus {
80            code: status.code(),
81            message: status.message().to_owned(),
82        }
83    }
84}
85
86pub const FROM_UTF8: &str = "From UTF8 error";