fix payload error handler

This commit is contained in:
marin postma 2021-06-21 19:20:04 +02:00
parent 2bdaa70f31
commit 9092d35a3c
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9
2 changed files with 56 additions and 24 deletions

View File

@ -5,9 +5,10 @@ use actix_web as aweb;
use actix_web::body::Body; use actix_web::body::Body;
use actix_web::dev::BaseHttpResponseBuilder; use actix_web::dev::BaseHttpResponseBuilder;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use aweb::error::{JsonPayloadError, QueryPayloadError};
use meilisearch_error::{Code, ErrorCode}; use meilisearch_error::{Code, ErrorCode};
use milli::UserError; use milli::UserError;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum AuthenticationError { pub enum AuthenticationError {
@ -44,7 +45,8 @@ impl fmt::Display for ResponseError {
} }
impl<T> From<T> for ResponseError impl<T> From<T> for ResponseError
where T: ErrorCode where
T: ErrorCode,
{ {
fn from(other: T) -> Self { fn from(other: T) -> Self {
Self { Self {
@ -70,23 +72,6 @@ impl aweb::error::ResponseError for ResponseError {
} }
} }
#[derive(Debug)]
struct PayloadError<E>(E);
impl<E: Error> fmt::Display for PayloadError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl<E: Error> Error for PayloadError<E> {}
impl<E: Error> ErrorCode for PayloadError<E> {
fn error_code(&self) -> Code {
Code::Internal
}
}
macro_rules! internal_error { macro_rules! internal_error {
($target:ty : $($other:path), *) => { ($target:ty : $($other:path), *) => {
$( $(
@ -140,10 +125,57 @@ impl ErrorCode for MilliError<'_> {
} }
} }
impl fmt::Display for PayloadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PayloadError::Json(e) => e.fmt(f),
PayloadError::Query(e) => e.fmt(f),
}
}
}
#[derive(Debug)]
pub enum PayloadError {
Json(JsonPayloadError),
Query(QueryPayloadError),
}
impl Error for PayloadError {}
impl ErrorCode for PayloadError {
fn error_code(&self) -> Code {
match self {
PayloadError::Json(err) => match err {
JsonPayloadError::Deserialize(_) => Code::BadRequest,
JsonPayloadError::Overflow => Code::PayloadTooLarge,
JsonPayloadError::ContentType => Code::UnsupportedMediaType,
JsonPayloadError::Payload(_) => Code::BadRequest,
JsonPayloadError::Serialize(_) => Code::Internal,
_ => Code::Internal,
},
PayloadError::Query(err) => match err {
QueryPayloadError::Deserialize(_) => Code::BadRequest,
_ => Code::Internal,
},
}
}
}
impl From<JsonPayloadError> for PayloadError {
fn from(other: JsonPayloadError) -> Self {
Self::Json(other)
}
}
impl From<QueryPayloadError> for PayloadError {
fn from(other: QueryPayloadError) -> Self {
Self::Query(other)
}
}
pub fn payload_error_handler<E>(err: E) -> ResponseError pub fn payload_error_handler<E>(err: E) -> ResponseError
where where
E: Error + Sync + Send + 'static, E: Into<PayloadError>,
{ {
let error = PayloadError(err); err.into().into()
error.into()
} }

View File

@ -565,7 +565,7 @@ fn update_uuid_to_file_path(root: impl AsRef<Path>, uuid: Uuid) -> PathBuf {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::index_controller::{index_actor::MockIndexActorHandle, UpdateResult}; use crate::index_controller::{UpdateResult, index_actor::{MockIndexActorHandle, error::IndexActorError}};
use futures::future::ok; use futures::future::ok;
@ -644,7 +644,7 @@ mod test {
if processing.id() == 0 { if processing.id() == 0 {
Box::pin(ok(Ok(processing.process(UpdateResult::Other)))) Box::pin(ok(Ok(processing.process(UpdateResult::Other))))
} else { } else {
Box::pin(ok(Err(processing.fail(String::from("err"))))) Box::pin(ok(Err(processing.fail(IndexActorError::ExistingPrimaryKey.into()))))
} }
}); });