use poem::http::StatusCode;
use thiserror::Error;
#[derive(Debug, Error, Clone, Eq, PartialEq)]
pub enum ParseRequestError {
#[error("failed to parse param `{name}`: {reason}")]
ParseParam {
name: &'static str,
reason: String,
},
#[error("failed to parse request body: {reason}")]
ParseRequestBody {
reason: String,
},
#[error("the content type `{content_type}` is not supported.")]
ContentTypeNotSupported {
content_type: String,
},
#[error("expect a `Content-Type` header.")]
ExpectContentType,
#[error("poem extract error: {0}")]
Extractor(String),
#[error("authorization error")]
Authorization,
}
impl From<ParseRequestError> for poem::Error {
fn from(err: ParseRequestError) -> Self {
match &err {
ParseRequestError::ParseParam { .. } => poem::Error::bad_request(err),
ParseRequestError::ParseRequestBody { .. } => poem::Error::bad_request(err),
ParseRequestError::ContentTypeNotSupported { .. } => {
poem::Error::method_not_allowed(err)
}
ParseRequestError::ExpectContentType => poem::Error::method_not_allowed(err),
ParseRequestError::Extractor(_) => poem::Error::bad_request(err),
ParseRequestError::Authorization => poem::Error::new(StatusCode::UNAUTHORIZED),
}
}
}