2020-04-07 23:47:35 +08:00
use std ::fmt ;
2020-02-11 22:16:02 +08:00
use meilisearch_core ::{ FstError , HeedError } ;
2020-04-07 23:47:35 +08:00
use serde_json ::json ;
use actix_http ::{ ResponseBuilder , Response } ;
use actix_web ::http ::StatusCode ;
use actix_web ::* ;
use futures ::future ::{ ok , Ready } ;
2020-01-16 23:58:57 +08:00
2020-04-07 23:47:35 +08:00
// use crate::helpers::meilisearch::Error as SearchError;
2019-10-31 22:00:36 +08:00
2020-04-07 23:47:35 +08:00
#[ derive(Debug) ]
2019-10-31 22:00:36 +08:00
pub enum ResponseError {
Internal ( String ) ,
BadRequest ( String ) ,
InvalidToken ( String ) ,
NotFound ( String ) ,
IndexNotFound ( String ) ,
DocumentNotFound ( String ) ,
MissingHeader ( String ) ,
2020-04-07 02:05:02 +08:00
FilterParsing ( String ) ,
2019-10-31 22:00:36 +08:00
BadParameter ( String , String ) ,
2019-11-15 19:04:46 +08:00
OpenIndex ( String ) ,
2019-10-31 22:00:36 +08:00
CreateIndex ( String ) ,
2020-04-07 23:47:35 +08:00
CreateTransaction ,
CommitTransaction ,
Schema ,
InferPrimaryKey ,
2020-03-05 18:44:30 +08:00
InvalidIndexUid ,
2019-10-31 22:00:36 +08:00
Maintenance ,
}
2020-04-07 23:47:35 +08:00
impl fmt ::Display for ResponseError {
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
match self {
Self ::Internal ( err ) = > write! ( f , " Internal server error: {} " , err ) ,
Self ::BadRequest ( err ) = > write! ( f , " Bad request: {} " , err ) ,
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 ::CreateTransaction = > write! ( f , " Impossible to create transaction " ) ,
Self ::CommitTransaction = > write! ( f , " Impossible to commit transaction " ) ,
Self ::Schema = > write! ( f , " Internal schema is innaccessible " ) ,
Self ::InferPrimaryKey = > write! ( f , " Could not infer primary key " ) ,
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 22:00:36 +08:00
}
}
2020-04-07 23:47:35 +08:00
impl error ::ResponseError for ResponseError {
fn error_response ( & self ) -> 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 ,
Self ::InvalidToken ( _ ) = > StatusCode ::FORBIDDEN ,
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 ::CreateTransaction = > StatusCode ::INTERNAL_SERVER_ERROR ,
Self ::CommitTransaction = > StatusCode ::INTERNAL_SERVER_ERROR ,
Self ::Schema = > StatusCode ::INTERNAL_SERVER_ERROR ,
Self ::InferPrimaryKey = > StatusCode ::BAD_REQUEST ,
Self ::InvalidIndexUid = > StatusCode ::BAD_REQUEST ,
Self ::Maintenance = > StatusCode ::SERVICE_UNAVAILABLE ,
Self ::FilterParsing ( _ ) = > StatusCode ::BAD_REQUEST ,
2019-10-31 22:00:36 +08:00
}
}
}
2020-04-07 23:47:35 +08:00
// impl Responder for ResponseError {
// type Error = Error;
// type Future = Ready<Result<Response, Error>>;
2019-10-31 22:00:36 +08:00
2020-04-07 23:47:35 +08:00
// #[inline]
// fn respond_to(self, req: &HttpRequest) -> Self::Future {
// ok(self.error_response())
// }
// }
2020-01-16 23:58:57 +08:00
2020-01-30 01:30:21 +08:00
impl From < serde_json ::Error > for ResponseError {
fn from ( err : serde_json ::Error ) -> ResponseError {
2020-04-07 23:47:35 +08:00
ResponseError ::Internal ( err . to_string ( ) )
2020-01-30 01:30:21 +08:00
}
}
2020-01-16 23:58:57 +08:00
impl From < meilisearch_core ::Error > for ResponseError {
fn from ( err : meilisearch_core ::Error ) -> ResponseError {
2020-04-07 23:47:35 +08:00
ResponseError ::Internal ( err . to_string ( ) )
2020-01-16 23:58:57 +08:00
}
}
2020-02-03 05:59:19 +08:00
impl From < HeedError > for ResponseError {
fn from ( err : HeedError ) -> ResponseError {
2020-04-07 23:47:35 +08:00
ResponseError ::Internal ( err . to_string ( ) )
2020-01-16 23:58:57 +08:00
}
}
2020-02-03 05:59:19 +08:00
impl From < FstError > for ResponseError {
fn from ( err : FstError ) -> ResponseError {
2020-04-07 23:47:35 +08:00
ResponseError ::Internal ( err . to_string ( ) )
2020-01-16 23:58:57 +08:00
}
}
2020-04-07 23:47:35 +08:00
// impl From<SearchError> for ResponseError {
// fn from(err: SearchError) -> ResponseError {
// match err {
// SearchError::FilterParsing(s) => ResponseError::FilterParsing(s),
// _ => ResponseError::Internal(err),
// }
// }
// }
2020-01-16 23:58:57 +08:00
2020-01-30 01:30:21 +08:00
impl From < meilisearch_core ::settings ::RankingRuleConversionError > for ResponseError {
fn from ( err : meilisearch_core ::settings ::RankingRuleConversionError ) -> ResponseError {
2020-04-07 23:47:35 +08:00
ResponseError ::Internal ( err . to_string ( ) )
2020-01-16 23:58:57 +08:00
}
}