2020-12-12 20:32:06 +08:00
|
|
|
use std::error;
|
2021-06-15 03:26:35 +08:00
|
|
|
use std::error::Error;
|
2020-12-12 20:32:06 +08:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use actix_web as aweb;
|
2021-04-21 19:49:21 +08:00
|
|
|
use actix_web::body::Body;
|
|
|
|
use actix_web::dev::BaseHttpResponseBuilder;
|
2020-12-12 20:32:06 +08:00
|
|
|
use actix_web::http::StatusCode;
|
2021-03-16 01:11:10 +08:00
|
|
|
use meilisearch_error::{Code, ErrorCode};
|
2021-06-17 20:36:32 +08:00
|
|
|
use milli::UserError;
|
2021-03-16 01:11:10 +08:00
|
|
|
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
2020-12-22 21:02:41 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
use crate::index_controller::error::IndexControllerError;
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum AuthenticationError {
|
2021-06-21 19:57:32 +08:00
|
|
|
#[error("you must have an authorization token")]
|
2021-06-15 03:26:35 +08:00
|
|
|
MissingAuthorizationHeader,
|
2021-06-21 19:57:32 +08:00
|
|
|
#[error("invalid API key")]
|
2021-06-15 03:26:35 +08:00
|
|
|
InvalidToken(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ErrorCode for AuthenticationError {
|
|
|
|
fn error_code(&self) -> Code {
|
|
|
|
match self {
|
2021-06-15 23:39:07 +08:00
|
|
|
AuthenticationError::MissingAuthorizationHeader => Code::MissingAuthorizationHeader,
|
2021-06-15 03:26:35 +08:00
|
|
|
AuthenticationError::InvalidToken(_) => Code::InvalidToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ResponseError {
|
|
|
|
inner: Box<dyn ErrorCode>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for ResponseError {}
|
|
|
|
|
|
|
|
impl ErrorCode for ResponseError {
|
|
|
|
fn error_code(&self) -> Code {
|
|
|
|
self.inner.error_code()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ResponseError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.inner.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
macro_rules! response_error {
|
|
|
|
($($other:path), *) => {
|
|
|
|
$(
|
|
|
|
impl From<$other> for ResponseError {
|
|
|
|
fn from(error: $other) -> ResponseError {
|
|
|
|
ResponseError {
|
|
|
|
inner: Box::new(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 19:34:12 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
)*
|
|
|
|
};
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 23:39:07 +08:00
|
|
|
response_error!(IndexControllerError, AuthenticationError);
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
impl Serialize for ResponseError {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let struct_name = "ResponseError";
|
|
|
|
let field_count = 4;
|
|
|
|
|
|
|
|
let mut state = serializer.serialize_struct(struct_name, field_count)?;
|
|
|
|
state.serialize_field("message", &self.to_string())?;
|
|
|
|
state.serialize_field("errorCode", &self.error_name())?;
|
|
|
|
state.serialize_field("errorType", &self.error_type())?;
|
|
|
|
state.serialize_field("errorLink", &self.error_url())?;
|
|
|
|
state.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl aweb::error::ResponseError for ResponseError {
|
2021-04-21 19:49:21 +08:00
|
|
|
fn error_response(&self) -> aweb::BaseHttpResponse<Body> {
|
|
|
|
let json = serde_json::to_vec(self).unwrap();
|
|
|
|
BaseHttpResponseBuilder::new(self.status_code()).body(json)
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
self.http_status()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-06-15 03:26:35 +08:00
|
|
|
struct PayloadError<E>(E);
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
impl<E: Error> fmt::Display for PayloadError<E> {
|
2020-12-12 20:32:06 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-06-15 03:26:35 +08:00
|
|
|
std::fmt::Display::fmt(&self.0, f)
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
impl<E: Error> Error for PayloadError<E> {}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
impl<E: Error> ErrorCode for PayloadError<E> {
|
|
|
|
fn error_code(&self) -> Code {
|
|
|
|
Code::Internal
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
impl<E> From<PayloadError<E>> for ResponseError
|
2021-06-15 23:39:07 +08:00
|
|
|
where
|
|
|
|
E: Error + Sync + Send + 'static,
|
2021-06-15 03:26:35 +08:00
|
|
|
{
|
|
|
|
fn from(other: PayloadError<E>) -> Self {
|
|
|
|
ResponseError {
|
|
|
|
inner: Box::new(other),
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub fn payload_error_handler<E>(err: E) -> ResponseError
|
2021-06-15 23:39:07 +08:00
|
|
|
where
|
|
|
|
E: Error + Sync + Send + 'static,
|
2021-06-15 03:26:35 +08:00
|
|
|
{
|
|
|
|
let error = PayloadError(err);
|
2020-12-12 20:32:06 +08:00
|
|
|
error.into()
|
|
|
|
}
|
2021-06-15 23:55:27 +08: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 20:36:32 +08: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.
|
|
|
|
UserError::AttributeLimitReached
|
|
|
|
| UserError::Csv(_)
|
|
|
|
| UserError::SerdeJson(_)
|
|
|
|
| UserError::MaxDatabaseSizeReached
|
|
|
|
| UserError::InvalidCriterionName { .. }
|
|
|
|
| UserError::InvalidDocumentId { .. }
|
|
|
|
| UserError::InvalidStoreFile
|
|
|
|
| UserError::NoSpaceLeftOnDevice
|
|
|
|
| UserError::DocumentLimitReached => todo!(),
|
|
|
|
UserError::InvalidFilter(_) => Code::Filter,
|
|
|
|
UserError::InvalidFilterAttribute(_) => Code::Filter,
|
|
|
|
UserError::MissingDocumentId { .. } => Code::MissingDocumentId,
|
|
|
|
UserError::MissingPrimaryKey => Code::MissingPrimaryKey,
|
|
|
|
UserError::PrimaryKeyCannotBeChanged => Code::PrimaryKeyAlreadyPresent,
|
|
|
|
UserError::PrimaryKeyCannotBeReset => Code::PrimaryKeyAlreadyPresent,
|
|
|
|
UserError::UnknownInternalDocumentId { .. } => Code::DocumentNotFound,
|
|
|
|
}
|
2021-06-17 20:38:52 +08:00
|
|
|
}
|
2021-06-17 20:36:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|