flow_guard/
error.rs

1/* * Created and Developed by: Cleiton Augusto Correa Bezerra */
2#[cfg(feature = "axum")]
3use axum::{
4    http::StatusCode,
5    response::{IntoResponse, Response},
6};
7use thiserror::Error;
8
9#[derive(Error, Debug)]
10pub enum FlowError<E> {
11    #[error("Request dropped due to high load")]
12    Dropped,
13    #[error("FlowGuard semaphore closed")]
14    Closed,
15    #[error("Application error: {0}")]
16    AppError(#[from] E),
17}
18
19#[cfg(feature = "axum")]
20impl<E> IntoResponse for FlowError<E>
21where
22    E: std::fmt::Display,
23{
24    fn into_response(self) -> Response {
25        match self {
26            Self::Dropped => (
27                StatusCode::SERVICE_UNAVAILABLE,
28                "Service Overloaded - Try again later",
29            )
30                .into_response(),
31            Self::Closed => (StatusCode::INTERNAL_SERVER_ERROR, "FlowGuard Closed").into_response(),
32            Self::AppError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
33        }
34    }
35}