pub use int_enum::IntEnum;
use std::error::Error;
use std::fmt::{Debug, Display, Error as FmtError, Formatter};
use std::time::SystemTimeError;
use url::ParseError;
#[repr(i16)]
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone, IntEnum)]
pub enum ErrorCode {
UrlParseError = -4,
ConnectionError = -3,
Timeout = -2,
Unknown = -1,
Continue = 100,
OK = 200,
Created = 201,
Accepted = 202,
NoContent = 204,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
PayloadTooLarge = 413,
URITooLong = 414,
UnsupportedMediaType = 415,
RangeNotSatisfiable = 416,
ExpectationFailed = 417,
ImATeapot = 418,
MisdirectedRequest = 421,
UnprocessableEntity = 422,
Locked = 423,
FailedDependency = 424,
TooEarly = 425,
UpgradeRequired = 426,
PreconditionRequired = 428,
TooManyRequests = 429,
RequestHeaderFieldsTooLarge = 431,
UnavailableForLegalReasons = 451,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HTTPVersionNotSupported = 505,
VariantAlsoNegotiates = 506,
InsufficientStorage = 507,
LoopDetected = 508,
NotExtended = 510,
NetworkAuthenticationRequired = 511,
}
#[derive(PartialEq, Debug, Clone)]
pub struct ReductError {
pub status: ErrorCode,
pub message: String,
}
impl Display for ReductError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
write!(f, "[{:?}] {}", self.status, self.message)
}
}
impl Display for ErrorCode {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
write!(f, "{}", self.int_value())
}
}
impl From<std::io::Error> for ReductError {
fn from(err: std::io::Error) -> Self {
ReductError {
status: ErrorCode::InternalServerError,
message: err.to_string(),
}
}
}
impl From<SystemTimeError> for ReductError {
fn from(err: SystemTimeError) -> Self {
ReductError {
status: ErrorCode::InternalServerError,
message: err.to_string(),
}
}
}
impl From<ParseError> for ReductError {
fn from(err: ParseError) -> Self {
ReductError {
status: ErrorCode::UrlParseError,
message: err.to_string(),
}
}
}
impl Error for ReductError {
fn description(&self) -> &str {
&self.message
}
}
impl ReductError {
pub fn new(status: ErrorCode, message: &str) -> Self {
ReductError {
status,
message: message.to_string(),
}
}
pub fn status(&self) -> ErrorCode {
self.status
}
pub fn message(&self) -> &str {
&self.message
}
pub fn ok() -> ReductError {
ReductError {
status: ErrorCode::OK,
message: "".to_string(),
}
}
pub fn no_content(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::NoContent,
message: msg.to_string(),
}
}
pub fn not_found(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::NotFound,
message: msg.to_string(),
}
}
pub fn conflict(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::Conflict,
message: msg.to_string(),
}
}
pub fn bad_request(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::BadRequest,
message: msg.to_string(),
}
}
pub fn unauthorized(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::Unauthorized,
message: msg.to_string(),
}
}
pub fn forbidden(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::Forbidden,
message: msg.to_string(),
}
}
pub fn unprocessable_entity(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::UnprocessableEntity,
message: msg.to_string(),
}
}
pub fn too_early(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::TooEarly,
message: msg.to_string(),
}
}
pub fn internal_server_error(msg: &str) -> ReductError {
ReductError {
status: ErrorCode::InternalServerError,
message: msg.to_string(),
}
}
}