fix tests, restore behavior

This commit is contained in:
mpostma 2020-05-22 18:04:23 +02:00
parent c771694623
commit 3bd15a4195
16 changed files with 218 additions and 145 deletions

View file

@ -12,9 +12,80 @@ enum ErrorCategory {
}
pub enum Code {
BadParameter,
BadRequest,
CreateIndex,
DocumentNotFound,
IndexNotFound,
Internal,
InvalidIndexUid,
InvalidToken,
Maintenance,
MissingAuthorizationHeader,
MissingHeader,
NotFound,
OpenIndex,
RetrieveDocument,
SearchDocuments,
PayloadTooLarge,
UnsupportedMediaType,
Other,
}
impl Code {
/// ascociate a `Code` variant to the actual ErrCode
fn err_code(&self) -> ErrCode {
use Code::*;
match self {
BadParameter
| BadRequest
| CreateIndex
| InvalidIndexUid
| OpenIndex
| RetrieveDocument
| SearchDocuments => ErrCode::bad_request(false, false, ErrorCategory::None, 0),
DocumentNotFound
| IndexNotFound
| NotFound => ErrCode::not_found(false, false, ErrorCategory::None, 0),
InvalidToken
| MissingHeader => ErrCode::unauthorized(false, false, ErrorCategory::None, 0),
MissingAuthorizationHeader => ErrCode::forbidden(false, false, ErrorCategory::None, 0),
Internal => ErrCode::internal(false, false, ErrorCategory::None, 0),
Maintenance => ErrCode::service_unavailable(false, false, ErrorCategory::None, 0),
PayloadTooLarge => ErrCode::payload_too_large(false, false, ErrorCategory::None, 0),
UnsupportedMediaType => ErrCode::unsupported_media_type(false, false, ErrorCategory::None, 0),
_ => ErrCode::not_found(false, false, ErrorCategory::None, 0),
}
}
/// return the HTTP status code ascociated with the `Code`
pub fn http(&self) -> StatusCode {
self.err_code().http_code
}
/// returns internal error code, in the form:
/// `EPFCNN`
/// - E: plain letter "E", to mark an error code, future main introduce W for warning
/// - P: scope of the error, 0 for private, 1 for public. Private are error that make no sense
/// reporting to the user, they are internal errors, and there is nothing the user can do about
/// them. they are nonetheless returned, without a message, for assistance purpose.
/// - F: 0 or 1, report if the error is fatal.
/// - C: error category, number in 0-9, the category of the error. Categories are still to be determined, input is required.
/// - NN: The error number, two digits, within C.
pub fn internal(&self) -> String {
let ErrCode { public, fatal, category, code, .. } = self.err_code();
format!("E{}{}{}{}",
public as u16,
fatal as u16,
category as u16,
code)
}
}
impl fmt::Display for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.internal().fmt(f)
@ -56,6 +127,42 @@ impl ErrCode {
ErrCode::new(public, fatal, StatusCode::INTERNAL_SERVER_ERROR, category, code)
}
pub fn bad_request(
public: bool,
fatal: bool,
category: ErrorCategory,
code: u16
) -> ErrCode {
ErrCode::new(public, fatal, StatusCode::BAD_REQUEST, category, code)
}
pub fn unsupported_media_type(
public: bool,
fatal: bool,
category: ErrorCategory,
code: u16
) -> ErrCode {
ErrCode::new(public, fatal, StatusCode::UNSUPPORTED_MEDIA_TYPE, category, code)
}
pub fn payload_too_large(
public: bool,
fatal: bool,
category: ErrorCategory,
code: u16
) -> ErrCode {
ErrCode::new(public, fatal, StatusCode::PAYLOAD_TOO_LARGE, category, code)
}
pub fn service_unavailable(
public: bool,
fatal: bool,
category: ErrorCategory,
code: u16
) -> ErrCode {
ErrCode::new(public, fatal, StatusCode::SERVICE_UNAVAILABLE, category, code)
}
pub fn forbidden(
public: bool,
fatal: bool,
@ -83,40 +190,3 @@ impl ErrCode {
ErrCode::new(public, fatal, StatusCode::NOT_FOUND, category, code)
}
}
impl Code {
/// ascociate a `Code` variant to the actual ErrCode
fn err_code(&self) -> ErrCode {
use Code::*;
match self {
Other => ErrCode::not_found(false, false, ErrorCategory::None, 0),
}
}
/// return the HTTP status code ascociated with the `Code`
pub fn http(&self) -> StatusCode {
self.err_code().http_code
}
/// returns internal error code, in the form:
/// `EPFCNN`
/// - E: plain letter "E", to mark an error code, future main introduce W for warning
/// - P: scope of the error, 0 for private, 1 for public. Private are error that make no sense
/// reporting to the user, they are internal errors, and there is nothing the user can do about
/// them. they are nonetheless returned, without a message, for assistance purpose.
/// - F: 0 or 1, report if the error is fatal.
/// - C: error category, number in 0-9, the category of the error. Categories are still to be determined, input is required.
/// - NN: The error number, two digits, within C.
pub fn internal(&self) -> String {
let ErrCode { public, fatal, category, code, .. } = self.err_code();
format!("E{}{}{}{}",
public as u16,
fatal as u16,
category as u16,
code)
}
}