Skip to main content

humblegen_rt/
handler.rs

1//! `HANDLER` Types used by a handler implementation, re-exported by generated code.
2
3use core::fmt::Display;
4
5/// The response type returned by implementors of a humblegen service trait function.
6pub type HandlerResponse<T> = Result<T, ServiceError>;
7
8/// A service-level error.
9///
10/// This type is returned by implementors of a humblegen service trait function
11/// as part of a `HandlerResponse`.
12///
13/// The runtime converts it to a `super::service_protocol::ServiceError`.
14#[derive(Debug)]
15pub enum ServiceError {
16    Authentication,
17    Authorization,
18    Internal(Box<dyn std::error::Error + Send + Sync>),
19}
20
21impl Display for ServiceError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            ServiceError::Authentication => write!(f, "authentication error"),
25            ServiceError::Authorization => write!(f, "not authorized"),
26            ServiceError::Internal(e) => write!(f, "internal server error: {:?}", e),
27        }
28    }
29}