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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::{error::Error as StdError, fmt::Display, str::FromStr};

use bytes::Bytes;

use crate::{response::BoxResponse, BoxError, Response};

crate::include_proto!("hrpc.v1");

/// Represents a hRPC error identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum HrpcErrorIdentifier {
    /// Endpoint was not implemented by server.
    NotImplemented,
    /// Reached resource quota or rate limited by server.
    ResourceExhausted,
    /// An error occured in server.
    InternalServerError,
    /// The server could not be reached, most likely means the server is down.
    Unavailable,
    /// Specified endpoint was not found on server.
    NotFound,
}

impl From<HrpcErrorIdentifier> for String {
    fn from(id: HrpcErrorIdentifier) -> Self {
        id.as_id().to_string()
    }
}

impl AsRef<str> for HrpcErrorIdentifier {
    fn as_ref(&self) -> &str {
        self.as_id()
    }
}

/// Error produced when trying to parse a string as a [`HrpcErrorIdentifier`].
#[derive(Debug)]
#[non_exhaustive]
pub struct NotHrpcErrorIdentifier;

impl FromStr for HrpcErrorIdentifier {
    type Err = NotHrpcErrorIdentifier;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        compare_if_ret::compare_if_ret! {
            s,
            Self::NotImplemented,
            Self::ResourceExhausted,
            Self::InternalServerError,
            Self::Unavailable,
            Self::NotFound,
        }
    }
}

mod compare_if_ret {
    macro_rules! compare_if_ret {
        ($var:ident, $variant:expr, $( $variant2:expr, )+) => {
            if $variant.compare($var) {
                return Ok($variant);
            } $(
                else if $variant2.compare($var) {
                    return Ok($variant2);
                }
            )* else {
                return Err(NotHrpcErrorIdentifier);
            }
        };
    }

    pub(crate) use compare_if_ret;
}

impl HrpcErrorIdentifier {
    /// Return the string version of this hRPC identifier.
    pub const fn as_id(&self) -> &'static str {
        match self {
            Self::InternalServerError => "hrpc.internal-server-error",
            Self::ResourceExhausted => "hrpc.resource-exhausted",
            Self::NotImplemented => "hrpc.not-implemented",
            Self::Unavailable => "hrpc.unavailable",
            Self::NotFound => "hrpc.not-found",
        }
    }

    /// Compare this hRPC identifier with some string identifier to see if they match.
    pub fn compare(&self, identifier: impl AsRef<str>) -> bool {
        identifier.as_ref() == self.as_id()
    }
}

impl Error {
    /// Create a new hRPC error representing a not implemented endpoint ([`HrpcErrorIdentifier::NotImplemented`]).
    pub fn new_not_implemented(message: impl Into<String>) -> Self {
        Self::default()
            .with_identifier(HrpcErrorIdentifier::NotImplemented)
            .with_message(message)
    }

    /// Create a new hRPC error representing resource exhaustion by a client ([`HrpcErrorIdentifier::ResourceExhausted`]).
    pub fn new_resource_exhausted(message: impl Into<String>) -> Self {
        Self::default()
            .with_identifier(HrpcErrorIdentifier::ResourceExhausted)
            .with_message(message)
    }

    /// Create a new hRPC error representing an internal server error ([`HrpcErrorIdentifier::InternalServerError`]).
    pub fn new_internal_server_error(message: impl Into<String>) -> Self {
        Self::default()
            .with_identifier(HrpcErrorIdentifier::InternalServerError)
            .with_message(message)
    }

    /// Create a new hRPC error representing a not found error ([`HrpcErrorIdentifier::NotFound`]).
    pub fn new_not_found(message: impl Into<String>) -> Self {
        Self::default()
            .with_identifier(HrpcErrorIdentifier::NotFound)
            .with_message(message)
    }

    /// Set the "more details" of this hRPC error.
    pub fn with_details(mut self, details: impl Into<Bytes>) -> Self {
        self.more_details = details.into();
        self
    }

    /// Set the "identifier" of this hRPC error.
    pub fn with_identifier(mut self, identifier: impl Into<String>) -> Self {
        self.identifier = identifier.into();
        self
    }

    /// Set the "human message" of this hRPC error.
    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.human_message = message.into();
        self
    }
}

impl From<&'static str> for Error {
    fn from(msg: &'static str) -> Self {
        Error::default()
            .with_identifier(HrpcErrorIdentifier::InternalServerError)
            .with_message(msg)
    }
}

impl From<(&'static str, &'static str)> for Error {
    fn from((id, msg): (&'static str, &'static str)) -> Self {
        Error::default().with_identifier(id).with_message(msg)
    }
}

impl From<BoxError> for Error {
    fn from(err: BoxError) -> Self {
        Error::default().with_message(err.to_string())
    }
}

impl From<(&'static str, BoxError)> for Error {
    fn from((id, err): (&'static str, BoxError)) -> Self {
        Error::default()
            .with_identifier(id)
            .with_message(err.to_string())
    }
}

impl From<Error> for Response<Error> {
    fn from(err: Error) -> Self {
        let mut resp = Response::new(&err);
        resp.extensions_mut().insert(err);
        resp
    }
}

impl From<Error> for BoxResponse {
    fn from(err: Error) -> Self {
        Response::<Error>::from(err).map::<()>()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "error '{}': {}", self.identifier, self.human_message)
    }
}

impl StdError for Error {}