MeiliSearch/meilisearch-http/src/error.rs

172 lines
5.3 KiB
Rust
Raw Normal View History

use std::error::Error;
2020-12-12 13:32:06 +01:00
use std::fmt;
use actix_web as aweb;
2021-04-21 13:49:21 +02:00
use actix_web::body::Body;
2020-12-12 13:32:06 +01:00
use actix_web::http::StatusCode;
2021-09-08 12:34:56 +02:00
use actix_web::HttpResponseBuilder;
2021-06-21 19:20:04 +02:00
use aweb::error::{JsonPayloadError, QueryPayloadError};
2021-03-15 18:11:10 +01:00
use meilisearch_error::{Code, ErrorCode};
2021-06-17 14:36:32 +02:00
use milli::UserError;
2021-06-21 19:20:04 +02:00
use serde::{Deserialize, Serialize};
2021-06-21 18:42:47 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
2020-12-12 13:32:06 +01:00
pub struct ResponseError {
2021-06-21 18:42:47 +02:00
#[serde(skip)]
code: StatusCode,
message: String,
error_code: String,
error_type: String,
error_link: String,
2020-12-12 13:32:06 +01:00
}
impl fmt::Display for ResponseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2021-06-21 18:42:47 +02:00
self.message.fmt(f)
2020-12-12 13:32:06 +01:00
}
}
2021-06-21 18:42:47 +02:00
impl<T> From<T> for ResponseError
2021-06-21 19:20:04 +02:00
where
T: ErrorCode,
2021-06-21 18:42:47 +02:00
{
fn from(other: T) -> Self {
Self {
code: other.http_status(),
message: other.to_string(),
error_code: other.error_name(),
error_type: other.error_type(),
error_link: other.error_url(),
}
2020-12-12 13:32:06 +01:00
}
}
impl aweb::error::ResponseError for ResponseError {
2021-09-08 12:34:56 +02:00
fn error_response(&self) -> aweb::HttpResponse<Body> {
2021-04-21 13:49:21 +02:00
let json = serde_json::to_vec(self).unwrap();
2021-09-08 12:34:56 +02:00
HttpResponseBuilder::new(self.status_code())
2021-06-21 18:48:05 +02:00
.content_type("application/json")
.body(json)
2020-12-12 13:32:06 +01:00
}
fn status_code(&self) -> StatusCode {
2021-06-21 18:42:47 +02:00
self.code
2020-12-12 13:32:06 +01:00
}
}
2021-06-15 17:55:27 +02:00
macro_rules! internal_error {
($target:ty : $($other:path), *) => {
$(
impl From<$other> for $target {
fn from(other: $other) -> Self {
Self::Internal(Box::new(other))
}
}
)*
}
}
2021-06-17 14:36:32 +02:00
#[derive(Debug)]
pub struct MilliError<'a>(pub &'a milli::Error);
impl Error for MilliError<'_> {}
impl fmt::Display for MilliError<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl ErrorCode for MilliError<'_> {
fn error_code(&self) -> Code {
match self.0 {
milli::Error::InternalError(_) => Code::Internal,
milli::Error::IoError(_) => Code::Internal,
milli::Error::UserError(ref error) => {
match error {
// TODO: wait for spec for new error codes.
2021-06-23 14:48:33 +02:00
UserError::Csv(_)
2021-06-17 14:36:32 +02:00
| UserError::SerdeJson(_)
| UserError::MaxDatabaseSizeReached
| UserError::InvalidCriterionName { .. }
| UserError::InvalidDocumentId { .. }
| UserError::InvalidStoreFile
| UserError::NoSpaceLeftOnDevice
2021-09-08 16:54:39 +02:00
| UserError::InvalidAscDescSyntax { .. }
2021-06-21 18:42:47 +02:00
| UserError::DocumentLimitReached => Code::Internal,
2021-06-22 11:58:01 +02:00
UserError::AttributeLimitReached => Code::MaxFieldsLimitExceeded,
2021-06-17 14:36:32 +02:00
UserError::InvalidFilter(_) => Code::Filter,
UserError::InvalidFilterAttribute(_) => Code::Filter,
2021-09-08 16:54:39 +02:00
UserError::InvalidSortName { .. } => Code::Sort,
2021-06-17 14:36:32 +02:00
UserError::MissingDocumentId { .. } => Code::MissingDocumentId,
UserError::MissingPrimaryKey => Code::MissingPrimaryKey,
UserError::PrimaryKeyCannotBeChanged => Code::PrimaryKeyAlreadyPresent,
UserError::PrimaryKeyCannotBeReset => Code::PrimaryKeyAlreadyPresent,
2021-09-08 16:54:39 +02:00
UserError::SortRankingRuleMissing => Code::Sort,
2021-06-17 14:36:32 +02:00
UserError::UnknownInternalDocumentId { .. } => Code::DocumentNotFound,
2021-06-23 18:40:19 +02:00
UserError::InvalidFacetsDistribution { .. } => Code::BadRequest,
UserError::InvalidSortableAttribute { .. } => Code::Sort,
2021-06-17 14:36:32 +02:00
}
2021-06-17 14:38:52 +02:00
}
2021-06-17 14:36:32 +02:00
}
}
}
2021-06-21 18:42:47 +02:00
2021-06-21 19:20:04 +02:00
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 {
2021-09-08 12:34:56 +02:00
JsonPayloadError::Overflow { .. } => Code::PayloadTooLarge,
2021-06-21 19:20:04 +02:00
JsonPayloadError::ContentType => Code::UnsupportedMediaType,
2021-06-23 14:48:33 +02:00
JsonPayloadError::Payload(aweb::error::PayloadError::Overflow) => {
Code::PayloadTooLarge
}
JsonPayloadError::Deserialize(_) | JsonPayloadError::Payload(_) => Code::BadRequest,
2021-06-21 19:20:04 +02:00
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)
}
}
2021-06-21 18:42:47 +02:00
pub fn payload_error_handler<E>(err: E) -> ResponseError
where
2021-06-21 19:20:04 +02:00
E: Into<PayloadError>,
2021-06-21 18:42:47 +02:00
{
2021-06-21 19:20:04 +02:00
err.into().into()
2021-06-21 18:42:47 +02:00
}