diff --git a/src/error.rs b/src/error.rs index 0f03bcdd..606a9258 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ //! Error and Result module -use std::{fmt, result}; +use std::{io, fmt, result}; use std::str::Utf8Error; use std::string::FromUtf8Error; use std::io::Error as IoError; @@ -92,7 +92,19 @@ impl ResponseError for JsonError {} impl ResponseError for HttpError {} /// Return `InternalServerError` for `io::Error` -impl ResponseError for IoError {} +impl ResponseError for io::Error { + + fn error_response(&self) -> HttpResponse { + match self.kind() { + io::ErrorKind::NotFound => + HttpResponse::new(StatusCode::NOT_FOUND, Body::Empty), + io::ErrorKind::PermissionDenied => + HttpResponse::new(StatusCode::FORBIDDEN, Body::Empty), + _ => + HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty) + } + } +} /// `InternalServerError` for `InvalidHeaderValue` impl ResponseError for header::InvalidHeaderValue {} diff --git a/src/recognizer.rs b/src/recognizer.rs index 9dec5735..7531b43a 100644 --- a/src/recognizer.rs +++ b/src/recognizer.rs @@ -172,7 +172,7 @@ macro_rules! FROM_STR { type Err = BadRequest<<$type as FromStr>::Err>; fn from_param(val: &str) -> Result { - <$type as FromStr>::from_str(val).map_err(|e| BadRequest(e)) + <$type as FromStr>::from_str(val).map_err(BadRequest) } } } diff --git a/src/staticfiles.rs b/src/staticfiles.rs index 858ee5f6..0618fa40 100644 --- a/src/staticfiles.rs +++ b/src/staticfiles.rs @@ -11,7 +11,7 @@ use mime_guess::get_mime_type; use route::Handler; use httprequest::HttpRequest; use httpresponse::HttpResponse; -use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden}; +use httpcodes::{HTTPOk, HTTPNotFound}; /// Static files handling /// @@ -149,26 +149,10 @@ impl Handler for StaticFiles { // full filepath let idx = if filepath.starts_with('/') { 1 } else { 0 }; - let filename = match self.directory.join(&filepath[idx..]).canonicalize() { - Ok(fname) => fname, - Err(err) => return match err.kind() { - io::ErrorKind::NotFound => Ok(HTTPNotFound.into()), - io::ErrorKind::PermissionDenied => Ok(HTTPForbidden.into()), - _ => Err(err), - } - }; + let filename = self.directory.join(&filepath[idx..]).canonicalize()?; if filename.is_dir() { - match self.index( - &req.path()[..req.prefix_len()], &filepath[idx..], &filename) - { - Ok(resp) => Ok(resp), - Err(err) => match err.kind() { - io::ErrorKind::NotFound => Ok(HTTPNotFound.into()), - io::ErrorKind::PermissionDenied => Ok(HTTPForbidden.into()), - _ => Err(err), - } - } + self.index(&req.path()[..req.prefix_len()], &filepath[idx..], &filename) } else { let mut resp = HTTPOk.build(); if let Some(ext) = filename.extension() {