From 4a40b026a4d7010816cdd177258743781547a6f4 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 8 Dec 2017 15:52:46 -0800 Subject: [PATCH] more error wrappers --- src/error.rs | 95 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 20 deletions(-) diff --git a/src/error.rs b/src/error.rs index a45b6b6f0..33cb25bab 100644 --- a/src/error.rs +++ b/src/error.rs @@ -426,6 +426,34 @@ impl From for UrlGenerationError { } } +macro_rules! ERROR_WRAP { + ($type:ty, $status:expr) => { + unsafe impl Sync for $type {} + unsafe impl Send for $type {} + + impl $type { + pub fn cause(&self) -> &T { + &self.0 + } + } + + impl Fail for $type {} + impl fmt::Display for $type { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self.0) + } + } + + impl ResponseError for $type + where T: Send + Sync + fmt::Debug + 'static, + { + fn error_response(&self) -> HttpResponse { + HttpResponse::new($status, Body::Empty) + } + } + + } +} /// Helper type that can wrap any error and generate *BAD REQUEST* response. /// @@ -445,30 +473,57 @@ impl From for UrlGenerationError { /// ``` #[derive(Debug)] pub struct ErrorBadRequest(pub T); +ERROR_WRAP!(ErrorBadRequest, StatusCode::BAD_REQUEST); -unsafe impl Sync for ErrorBadRequest {} -unsafe impl Send for ErrorBadRequest {} +#[derive(Debug)] +/// Helper type that can wrap any error and generate *UNAUTHORIZED* response. +pub struct ErrorUnauthorized(pub T); +ERROR_WRAP!(ErrorUnauthorized, StatusCode::UNAUTHORIZED); -impl ErrorBadRequest { - pub fn cause(&self) -> &T { - &self.0 - } -} +#[derive(Debug)] +/// Helper type that can wrap any error and generate *FORBIDDEN* response. +pub struct ErrorForbidden(pub T); +ERROR_WRAP!(ErrorForbidden, StatusCode::FORBIDDEN); -impl Fail for ErrorBadRequest {} -impl fmt::Display for ErrorBadRequest { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BadRequest({:?})", self.0) - } -} +#[derive(Debug)] +/// Helper type that can wrap any error and generate *NOT FOUND* response. +pub struct ErrorNotFound(pub T); +ERROR_WRAP!(ErrorNotFound, StatusCode::NOT_FOUND); -impl ResponseError for ErrorBadRequest - where T: Send + Sync + fmt::Debug + 'static, -{ - fn error_response(&self) -> HttpResponse { - HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty) - } -} +#[derive(Debug)] +/// Helper type that can wrap any error and generate *METHOD_NOT_ALLOWED* response. +pub struct ErrorMethodNotAllowed(pub T); +ERROR_WRAP!(ErrorMethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED); + +#[derive(Debug)] +/// Helper type that can wrap any error and generate *REQUEST_TIMEOUT* response. +pub struct ErrorRequestTimeout(pub T); +ERROR_WRAP!(ErrorRequestTimeout, StatusCode::REQUEST_TIMEOUT); + +#[derive(Debug)] +/// Helper type that can wrap any error and generate *CONFLICT* response. +pub struct ErrorConflict(pub T); +ERROR_WRAP!(ErrorConflict, StatusCode::CONFLICT); + +#[derive(Debug)] +/// Helper type that can wrap any error and generate *GONE* response. +pub struct ErrorGone(pub T); +ERROR_WRAP!(ErrorGone, StatusCode::GONE); + +#[derive(Debug)] +/// Helper type that can wrap any error and generate *PRECONDITION_FAILED* response. +pub struct ErrorPreconditionFailed(pub T); +ERROR_WRAP!(ErrorPreconditionFailed, StatusCode::PRECONDITION_FAILED); + +#[derive(Debug)] +/// Helper type that can wrap any error and generate *EXPECTATION_FAILED* response. +pub struct ErrorExpectationFailed(pub T); +ERROR_WRAP!(ErrorExpectationFailed, StatusCode::EXPECTATION_FAILED); + +#[derive(Debug)] +/// Helper type that can wrap any error and generate *INTERNAL_SERVER_ERROR* response. +pub struct ErrorInternalServerError(pub T); +ERROR_WRAP!(ErrorInternalServerError, StatusCode::INTERNAL_SERVER_ERROR); #[cfg(test)] mod tests {