You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
942 B
56 lines
942 B
4 years ago
|
//! 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<std::str::Utf8Error> for ApiError
|
||
|
{
|
||
|
fn from(_: std::str::Utf8Error) -> Self
|
||
|
{
|
||
|
Self::Body
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Handles API rejections
|
||
|
pub async fn rejection(err: Rejection) -> Result<impl Reply, Rejection>
|
||
|
{
|
||
|
if let Some(api) = err.find::<ApiError>() {
|
||
|
Ok(warp::reply::with_status(format!("ApiError: {}\n", api), api.error_code()))
|
||
|
} else {
|
||
|
Err(err)
|
||
|
}
|
||
|
}
|