mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 05:14:27 +01:00
ErrorCode improvements
This commit is contained in:
parent
51b3139c0b
commit
d9e2e1a177
@ -1,10 +1,29 @@
|
|||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use actix_http::http::StatusCode;
|
use actix_http::http::StatusCode;
|
||||||
|
|
||||||
pub trait ErrorCode: std::error::Error {
|
pub trait ErrorCode: std::error::Error {
|
||||||
fn error_code(&self) -> Code;
|
fn error_code(&self) -> Code;
|
||||||
|
|
||||||
|
/// returns the HTTP status code ascociated with the error
|
||||||
|
fn http_status(&self) -> StatusCode {
|
||||||
|
self.error_code().http()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// returns the doc url ascociated with the error
|
||||||
|
fn error_url(&self) -> String {
|
||||||
|
self.error_code().url()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// returns error name, used as error code
|
||||||
|
fn error_name(&self) -> String {
|
||||||
|
self.error_code().name()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return the error type
|
||||||
|
fn error_type(&self) -> String {
|
||||||
|
self.error_code().r#type()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ErrorType {
|
enum ErrorType {
|
||||||
@ -106,21 +125,22 @@ impl Code {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// return the HTTP status code ascociated with the `Code`
|
/// return the HTTP status code ascociated with the `Code`
|
||||||
pub fn http(&self) -> StatusCode {
|
fn http(&self) -> StatusCode {
|
||||||
self.err_code().status_code
|
self.err_code().status_code
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return error name, used as error code
|
/// return error name, used as error code
|
||||||
pub fn name(&self) -> String {
|
fn name(&self) -> String {
|
||||||
self.err_code().err_name.to_string()
|
self.err_code().err_name.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return the error type
|
/// return the error type
|
||||||
pub fn r#type(&self) -> String {
|
fn r#type(&self) -> String {
|
||||||
self.err_code().err_type.to_string()
|
self.err_code().err_type.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn url(&self) -> String {
|
/// return the doc url ascociated with the error
|
||||||
|
fn url(&self) -> String {
|
||||||
format!("docs.meilisearch.come/error/{}", self.name())
|
format!("docs.meilisearch.come/error/{}", self.name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,14 @@ pub struct ResponseError {
|
|||||||
inner: Box<dyn ErrorCode>,
|
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 {
|
impl fmt::Display for ResponseError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.inner.fmt(f)
|
self.inner.fmt(f)
|
||||||
@ -204,17 +212,16 @@ impl fmt::Display for Error {
|
|||||||
|
|
||||||
impl aweb::error::ResponseError for ResponseError {
|
impl aweb::error::ResponseError for ResponseError {
|
||||||
fn error_response(&self) -> aweb::HttpResponse {
|
fn error_response(&self) -> aweb::HttpResponse {
|
||||||
let error_code = self.inner.error_code();
|
|
||||||
ResponseBuilder::new(self.status_code()).json(json!({
|
ResponseBuilder::new(self.status_code()).json(json!({
|
||||||
"message": self.to_string(),
|
"message": self.to_string(),
|
||||||
"errorCode": error_code.name(),
|
"errorCode": self.error_name(),
|
||||||
"errorType": error_code.r#type(),
|
"errorType": self.error_type(),
|
||||||
"errorLink": error_code.url(),
|
"errorLink": self.error_url(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
self.inner.error_code().http()
|
self.http_status()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user