From 78d8d21196fede7f95776830481f9a31ddedb2ce Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 19 Nov 2017 17:26:31 -1000 Subject: [PATCH] cleanup error --- CHANGES.md | 4 ++-- src/error.rs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 939e0b8c..00136701 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,10 +9,10 @@ * Asynchronous middlewares -* Content compression/decompression (br, gzip, deflate) - * Refactor logger middleware +* Content compression/decompression (br, gzip, deflate) + ## 0.2.1 (2017-11-03) diff --git a/src/error.rs b/src/error.rs index db3087e0..939ddf43 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -//! Error and Result module. +//! Error and Result module use std::{fmt, result}; use std::str::Utf8Error; use std::string::FromUtf8Error; @@ -19,19 +19,28 @@ use httpresponse::HttpResponse; use httpcodes::{HTTPBadRequest, HTTPMethodNotAllowed}; /// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) -/// for actix web operations. +/// for actix web operations /// /// This typedef is generally used to avoid writing out `actix_web::error::Error` directly and /// is otherwise a direct mapping to `Result`. pub type Result = result::Result; -/// Actix web error. +/// Actix web error #[derive(Debug)] pub struct Error { cause: Box, } -/// Error that can be converted to HttpResponse +impl Error { + + /// Returns a reference to the underlying cause of this Error. + // this should return &Fail but needs this https://github.com/rust-lang/rust/issues/5665 + pub fn cause(&self) -> &ErrorResponse { + self.cause.as_ref() + } +} + +/// Error that can be converted to `HttpResponse` pub trait ErrorResponse: Fail { /// Create response for error @@ -48,7 +57,7 @@ impl fmt::Display for Error { } } -/// `HttpResponse` for `Error`. +/// `HttpResponse` for `Error` impl From for HttpResponse { fn from(err: Error) -> Self { err.cause.error_response() @@ -68,10 +77,10 @@ impl From for Error { // } // } -/// A set of errors that can occur during parsing HTTP streams. +/// A set of errors that can occur during parsing HTTP streams #[derive(Fail, Debug)] pub enum ParseError { - /// An invalid `Method`, such as `GE,T`. + /// An invalid `Method`, such as `GE.T`. #[fail(display="Invalid Method specified")] Method, /// An invalid `Uri`, such as `exam ple.domain`. @@ -144,7 +153,7 @@ impl From for ParseError { } #[derive(Fail, Debug)] -/// A set of errors that can occur during payload parsing. +/// A set of errors that can occur during payload parsing pub enum PayloadError { /// A payload reached EOF, but is not complete. #[fail(display="A payload reached EOF, but is not complete.")] @@ -210,7 +219,7 @@ impl From for HttpRangeError { } } -/// A set of errors that can occur during parsing multipart streams. +/// A set of errors that can occur during parsing multipart streams #[derive(Fail, Debug)] pub enum MultipartError { /// Content-Type header is not found @@ -334,6 +343,14 @@ mod tests { assert_eq!(format!("{}", e.cause().unwrap()), desc); } + #[test] + fn test_error_cause() { + let orig = io::Error::new(io::ErrorKind::Other, "other"); + let desc = orig.description().to_owned(); + let e = Error::from(orig); + assert_eq!(format!("{}", e.cause()), desc); + } + macro_rules! from { ($from:expr => $error:pat) => { match ParseError::from($from) {