//! API errors //use super::*; use std::{ error, fmt, }; use warp::{ Rejection, Reply, }; #[derive(Debug)] pub enum ApiError { Body, } impl ApiError { #[inline] fn error_code(&self) -> warp::http::StatusCode { status!(match self { Self::Body => 422, }) } } impl warp::reject::Reject for ApiError{} impl error::Error for ApiError{} impl std::fmt::Display for ApiError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Body => write!(f, "invalid data in request body"), } } } impl From for ApiError { fn from(_: std::str::Utf8Error) -> Self { Self::Body } } // Handles API rejections pub async fn rejection(err: Rejection) -> Result { if let Some(api) = err.find::() { Ok(warp::reply::with_status(format!("ApiError: {}\n", api), api.error_code())) } else { Err(err) } }