mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-26 20:15:07 +08:00
fix payload error handler
This commit is contained in:
parent
2bdaa70f31
commit
9092d35a3c
@ -5,9 +5,10 @@ use actix_web as aweb;
|
||||
use actix_web::body::Body;
|
||||
use actix_web::dev::BaseHttpResponseBuilder;
|
||||
use actix_web::http::StatusCode;
|
||||
use aweb::error::{JsonPayloadError, QueryPayloadError};
|
||||
use meilisearch_error::{Code, ErrorCode};
|
||||
use milli::UserError;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AuthenticationError {
|
||||
@ -44,7 +45,8 @@ impl fmt::Display for ResponseError {
|
||||
}
|
||||
|
||||
impl<T> From<T> for ResponseError
|
||||
where T: ErrorCode
|
||||
where
|
||||
T: ErrorCode,
|
||||
{
|
||||
fn from(other: T) -> 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 {
|
||||
($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
|
||||
where
|
||||
E: Error + Sync + Send + 'static,
|
||||
E: Into<PayloadError>,
|
||||
{
|
||||
let error = PayloadError(err);
|
||||
error.into()
|
||||
err.into().into()
|
||||
}
|
||||
|
@ -565,7 +565,7 @@ fn update_uuid_to_file_path(root: impl AsRef<Path>, uuid: Uuid) -> PathBuf {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::index_controller::{index_actor::MockIndexActorHandle, UpdateResult};
|
||||
use crate::index_controller::{UpdateResult, index_actor::{MockIndexActorHandle, error::IndexActorError}};
|
||||
|
||||
use futures::future::ok;
|
||||
|
||||
@ -644,7 +644,7 @@ mod test {
|
||||
if processing.id() == 0 {
|
||||
Box::pin(ok(Ok(processing.process(UpdateResult::Other))))
|
||||
} else {
|
||||
Box::pin(ok(Err(processing.fail(String::from("err")))))
|
||||
Box::pin(ok(Err(processing.fail(IndexActorError::ExistingPrimaryKey.into()))))
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user