meilisearch/meilisearch-http/src/error.rs

152 lines
5.8 KiB
Rust
Raw Normal View History

2020-12-12 20:32:06 +08:00
use actix_web as aweb;
2021-06-22 01:20:04 +08:00
use aweb::error::{JsonPayloadError, QueryPayloadError};
use meilisearch_types::document_formats::DocumentFormatError;
use meilisearch_types::error::{Code, ErrorCode, ResponseError};
use serde_json::Value;
2022-09-23 02:02:55 +08:00
use tokio::task::JoinError;
2021-09-30 16:26:30 +08:00
#[derive(Debug, thiserror::Error)]
pub enum MeilisearchHttpError {
#[error("A Content-Type header is missing. Accepted values for the Content-Type header are: {}",
2021-10-27 01:36:48 +08:00
.0.iter().map(|s| format!("`{}`", s)).collect::<Vec<_>>().join(", "))]
MissingContentType(Vec<String>),
#[error(
2021-10-27 01:36:48 +08:00
"The Content-Type `{0}` is invalid. Accepted values for the Content-Type header are: {}",
.1.iter().map(|s| format!("`{}`", s)).collect::<Vec<_>>().join(", ")
)]
InvalidContentType(String, Vec<String>),
#[error("Document `{0}` not found.")]
DocumentNotFound(String),
#[error("Invalid syntax for the filter parameter: `expected {}, found: {1}`.", .0.join(", "))]
InvalidExpression(&'static [&'static str], Value),
2022-10-22 19:26:25 +08:00
#[error("The provided payload reached the size limit.")]
PayloadTooLarge,
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
HeedError(#[from] meilisearch_types::heed::Error),
2022-09-23 02:02:55 +08:00
#[error(transparent)]
IndexScheduler(#[from] index_scheduler::Error),
#[error(transparent)]
Milli(#[from] meilisearch_types::milli::Error),
#[error(transparent)]
2022-09-23 02:02:55 +08:00
Payload(#[from] PayloadError),
#[error(transparent)]
2022-09-27 22:33:37 +08:00
FileStore(#[from] file_store::Error),
2022-09-23 02:02:55 +08:00
#[error(transparent)]
2022-09-27 22:33:37 +08:00
DocumentFormat(#[from] DocumentFormatError),
2022-09-23 02:02:55 +08:00
#[error(transparent)]
Join(#[from] JoinError),
2021-09-30 16:26:30 +08:00
}
impl ErrorCode for MeilisearchHttpError {
fn error_code(&self) -> Code {
match self {
MeilisearchHttpError::MissingContentType(_) => Code::MissingContentType,
MeilisearchHttpError::InvalidContentType(_, _) => Code::InvalidContentType,
MeilisearchHttpError::DocumentNotFound(_) => Code::DocumentNotFound,
MeilisearchHttpError::InvalidExpression(_, _) => Code::Filter,
2022-10-22 19:26:25 +08:00
MeilisearchHttpError::PayloadTooLarge => Code::PayloadTooLarge,
MeilisearchHttpError::SerdeJson(_) => Code::Internal,
MeilisearchHttpError::HeedError(_) => Code::Internal,
2022-09-23 02:02:55 +08:00
MeilisearchHttpError::IndexScheduler(e) => e.error_code(),
MeilisearchHttpError::Milli(e) => e.error_code(),
2022-09-23 02:02:55 +08:00
MeilisearchHttpError::Payload(e) => e.error_code(),
2022-09-27 22:33:37 +08:00
MeilisearchHttpError::FileStore(_) => Code::Internal,
2022-09-23 02:02:55 +08:00
MeilisearchHttpError::DocumentFormat(e) => e.error_code(),
MeilisearchHttpError::Join(_) => Code::Internal,
2021-09-30 16:26:30 +08:00
}
}
}
impl From<MeilisearchHttpError> for aweb::Error {
fn from(other: MeilisearchHttpError) -> Self {
aweb::Error::from(ResponseError::from(other))
}
}
2022-09-23 02:02:55 +08:00
impl From<aweb::error::PayloadError> for MeilisearchHttpError {
fn from(error: aweb::error::PayloadError) -> Self {
MeilisearchHttpError::Payload(PayloadError::Payload(error))
}
}
#[derive(Debug, thiserror::Error)]
2021-06-22 01:20:04 +08:00
pub enum PayloadError {
2022-09-23 02:02:55 +08:00
#[error(transparent)]
Payload(aweb::error::PayloadError),
#[error(transparent)]
2021-06-22 01:20:04 +08:00
Json(JsonPayloadError),
2022-09-23 02:02:55 +08:00
#[error(transparent)]
2021-06-22 01:20:04 +08:00
Query(QueryPayloadError),
#[error("The json payload provided is malformed. `{0}`.")]
MalformedPayload(serde_json::error::Error),
#[error("A json payload is missing.")]
MissingPayload,
2021-06-22 01:20:04 +08:00
}
impl ErrorCode for PayloadError {
fn error_code(&self) -> Code {
match self {
2022-09-23 02:02:55 +08:00
PayloadError::Payload(e) => match e {
2022-10-22 19:26:25 +08:00
aweb::error::PayloadError::Incomplete(_) => Code::Internal,
aweb::error::PayloadError::EncodingCorrupted => Code::Internal,
aweb::error::PayloadError::Overflow => Code::PayloadTooLarge,
aweb::error::PayloadError::UnknownLength => Code::Internal,
aweb::error::PayloadError::Http2Payload(_) => Code::Internal,
aweb::error::PayloadError::Io(_) => Code::Internal,
2022-09-23 02:02:55 +08:00
_ => todo!(),
},
2021-06-22 01:20:04 +08:00
PayloadError::Json(err) => match err {
2021-09-08 18:34:56 +08:00
JsonPayloadError::Overflow { .. } => Code::PayloadTooLarge,
2021-06-22 01:20:04 +08:00
JsonPayloadError::ContentType => Code::UnsupportedMediaType,
2021-06-23 20:48:33 +08:00
JsonPayloadError::Payload(aweb::error::PayloadError::Overflow) => {
Code::PayloadTooLarge
}
JsonPayloadError::Payload(_) => Code::BadRequest,
JsonPayloadError::Deserialize(_) => Code::BadRequest,
2021-06-22 01:20:04 +08:00
JsonPayloadError::Serialize(_) => Code::Internal,
_ => Code::Internal,
},
PayloadError::Query(err) => match err {
QueryPayloadError::Deserialize(_) => Code::BadRequest,
_ => Code::Internal,
},
PayloadError::MissingPayload => Code::MissingPayload,
PayloadError::MalformedPayload(_) => Code::MalformedPayload,
2021-06-22 01:20:04 +08:00
}
}
}
impl From<JsonPayloadError> for PayloadError {
fn from(other: JsonPayloadError) -> Self {
match other {
JsonPayloadError::Deserialize(e)
if e.classify() == serde_json::error::Category::Eof
&& e.line() == 1
&& e.column() == 0 =>
{
Self::MissingPayload
}
JsonPayloadError::Deserialize(e)
if e.classify() != serde_json::error::Category::Data =>
{
Self::MalformedPayload(e)
}
_ => Self::Json(other),
}
2021-06-22 01:20:04 +08:00
}
}
impl From<QueryPayloadError> for PayloadError {
fn from(other: QueryPayloadError) -> Self {
Self::Query(other)
}
}
impl From<PayloadError> for aweb::Error {
fn from(other: PayloadError) -> Self {
aweb::Error::from(ResponseError::from(other))
}
2021-06-22 00:42:47 +08:00
}