MeiliSearch/meilisearch-http/src/error.rs

73 lines
3.1 KiB
Rust
Raw Normal View History

use std::fmt;
2020-04-10 19:05:05 +02:00
use actix_http::ResponseBuilder;
2020-04-10 19:05:05 +02:00
use actix_web as aweb;
use actix_web::http::StatusCode;
2020-04-10 19:05:05 +02:00
use serde_json::json;
2019-10-31 15:00:36 +01:00
#[derive(Debug)]
2019-10-31 15:00:36 +01:00
pub enum ResponseError {
Internal(String),
BadRequest(String),
2020-04-14 18:00:35 +02:00
MissingAuthorizationHeader,
2019-10-31 15:00:36 +01:00
InvalidToken(String),
NotFound(String),
IndexNotFound(String),
DocumentNotFound(String),
MissingHeader(String),
2020-04-06 20:05:02 +02:00
FilterParsing(String),
2019-10-31 15:00:36 +01:00
BadParameter(String, String),
OpenIndex(String),
2019-10-31 15:00:36 +01:00
CreateIndex(String),
2020-03-05 11:44:30 +01:00
InvalidIndexUid,
2019-10-31 15:00:36 +01:00
Maintenance,
}
impl fmt::Display for ResponseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
2020-04-16 11:09:47 +02:00
Self::Internal(err) => write!(f, "{}", err),
Self::BadRequest(err) => write!(f, "{}", err),
2020-04-14 18:00:35 +02:00
Self::MissingAuthorizationHeader => write!(f, "You must have an authorization token"),
Self::InvalidToken(err) => write!(f, "Invalid API key: {}", err),
Self::NotFound(err) => write!(f, "{} not found", err),
Self::IndexNotFound(index_uid) => write!(f, "Index {} not found", index_uid),
Self::DocumentNotFound(document_id) => write!(f, "Document with id {} not found", document_id),
Self::MissingHeader(header) => write!(f, "Header {} is missing", header),
Self::BadParameter(param, err) => write!(f, "Url parameter {} error: {}", param, err),
Self::OpenIndex(err) => write!(f, "Impossible to open index; {}", err),
Self::CreateIndex(err) => write!(f, "Impossible to create index; {}", err),
Self::InvalidIndexUid => write!(f, "Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_)."),
Self::Maintenance => write!(f, "Server is in maintenance, please try again later"),
Self::FilterParsing(err) => write!(f, "parsing error: {}", err),
}
2019-10-31 15:00:36 +01:00
}
}
2020-04-09 10:39:34 +02:00
impl aweb::error::ResponseError for ResponseError {
fn error_response(&self) -> aweb::HttpResponse {
ResponseBuilder::new(self.status_code()).json(json!({
"message": self.to_string(),
}))
}
fn status_code(&self) -> StatusCode {
match *self {
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
2020-04-14 18:00:35 +02:00
Self::MissingAuthorizationHeader => StatusCode::FORBIDDEN,
Self::InvalidToken(_) => StatusCode::UNAUTHORIZED,
Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::IndexNotFound(_) => StatusCode::NOT_FOUND,
Self::DocumentNotFound(_) => StatusCode::NOT_FOUND,
Self::MissingHeader(_) => StatusCode::UNAUTHORIZED,
Self::BadParameter(_, _) => StatusCode::BAD_REQUEST,
Self::OpenIndex(_) => StatusCode::BAD_REQUEST,
Self::CreateIndex(_) => StatusCode::BAD_REQUEST,
Self::InvalidIndexUid => StatusCode::BAD_REQUEST,
Self::Maintenance => StatusCode::SERVICE_UNAVAILABLE,
Self::FilterParsing(_) => StatusCode::BAD_REQUEST,
2019-10-31 15:00:36 +01:00
}
}
}