2020-12-12 13:32:06 +01:00
|
|
|
use actix_web as aweb;
|
2021-06-21 19:20:04 +02:00
|
|
|
use aweb::error::{JsonPayloadError, QueryPayloadError};
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_error::{Code, ErrorCode, ResponseError};
|
2021-06-14 21:26:35 +02:00
|
|
|
|
2021-09-30 10:26:30 +02:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum MeilisearchHttpError {
|
2021-10-05 13:30:53 +02:00
|
|
|
#[error("A Content-Type header is missing. Accepted values for the Content-Type header are: {}",
|
2021-10-26 19:36:48 +02:00
|
|
|
.0.iter().map(|s| format!("`{}`", s)).collect::<Vec<_>>().join(", "))]
|
2021-10-05 13:30:53 +02:00
|
|
|
MissingContentType(Vec<String>),
|
|
|
|
#[error(
|
2021-10-26 19:36:48 +02:00
|
|
|
"The Content-Type `{0}` is invalid. Accepted values for the Content-Type header are: {}",
|
|
|
|
.1.iter().map(|s| format!("`{}`", s)).collect::<Vec<_>>().join(", ")
|
2021-10-05 13:30:53 +02:00
|
|
|
)]
|
|
|
|
InvalidContentType(String, Vec<String>),
|
2021-09-30 10:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ErrorCode for MeilisearchHttpError {
|
|
|
|
fn error_code(&self) -> Code {
|
|
|
|
match self {
|
2021-10-05 13:30:53 +02:00
|
|
|
MeilisearchHttpError::MissingContentType(_) => Code::MissingContentType,
|
|
|
|
MeilisearchHttpError::InvalidContentType(_, _) => Code::InvalidContentType,
|
2021-09-30 10:26:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:49:34 +02:00
|
|
|
impl From<MeilisearchHttpError> for aweb::Error {
|
|
|
|
fn from(other: MeilisearchHttpError) -> Self {
|
|
|
|
aweb::Error::from(ResponseError::from(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2021-06-21 19:20:04 +02:00
|
|
|
pub enum PayloadError {
|
2021-11-08 18:31:27 +01:00
|
|
|
#[error("{0}")]
|
2021-06-21 19:20:04 +02:00
|
|
|
Json(JsonPayloadError),
|
2021-11-08 18:31:27 +01:00
|
|
|
#[error("{0}")]
|
2021-06-21 19:20:04 +02:00
|
|
|
Query(QueryPayloadError),
|
2021-11-08 18:31:27 +01:00
|
|
|
#[error("The json payload provided is malformed. `{0}`.")]
|
|
|
|
MalformedPayload(serde_json::error::Error),
|
|
|
|
#[error("A json payload is missing.")]
|
|
|
|
MissingPayload,
|
2021-06-21 19:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-11-08 18:31:27 +01:00
|
|
|
JsonPayloadError::Payload(_) => Code::BadRequest,
|
|
|
|
JsonPayloadError::Deserialize(_) => 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,
|
|
|
|
},
|
2021-11-08 18:31:27 +01:00
|
|
|
PayloadError::MissingPayload => Code::MissingPayload,
|
|
|
|
PayloadError::MalformedPayload(_) => Code::MalformedPayload,
|
2021-06-21 19:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<JsonPayloadError> for PayloadError {
|
|
|
|
fn from(other: JsonPayloadError) -> Self {
|
2021-11-08 18:31:27 +01:00
|
|
|
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-21 19:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<QueryPayloadError> for PayloadError {
|
|
|
|
fn from(other: QueryPayloadError) -> Self {
|
|
|
|
Self::Query(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:49:34 +02:00
|
|
|
impl From<PayloadError> for aweb::Error {
|
|
|
|
fn from(other: PayloadError) -> Self {
|
|
|
|
aweb::Error::from(ResponseError::from(other))
|
|
|
|
}
|
2021-06-21 18:42:47 +02:00
|
|
|
}
|