1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

error response for io::Error

This commit is contained in:
Nikolay Kim 2017-12-02 14:58:22 -08:00
parent 29a26b3236
commit 187948ddd1
3 changed files with 18 additions and 22 deletions

View File

@ -1,5 +1,5 @@
//! Error and Result module //! Error and Result module
use std::{fmt, result}; use std::{io, fmt, result};
use std::str::Utf8Error; use std::str::Utf8Error;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use std::io::Error as IoError; use std::io::Error as IoError;
@ -92,7 +92,19 @@ impl ResponseError for JsonError {}
impl ResponseError for HttpError {} impl ResponseError for HttpError {}
/// Return `InternalServerError` for `io::Error` /// 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` /// `InternalServerError` for `InvalidHeaderValue`
impl ResponseError for header::InvalidHeaderValue {} impl ResponseError for header::InvalidHeaderValue {}

View File

@ -172,7 +172,7 @@ macro_rules! FROM_STR {
type Err = BadRequest<<$type as FromStr>::Err>; type Err = BadRequest<<$type as FromStr>::Err>;
fn from_param(val: &str) -> Result<Self, Self::Err> { fn from_param(val: &str) -> Result<Self, Self::Err> {
<$type as FromStr>::from_str(val).map_err(|e| BadRequest(e)) <$type as FromStr>::from_str(val).map_err(BadRequest)
} }
} }
} }

View File

@ -11,7 +11,7 @@ use mime_guess::get_mime_type;
use route::Handler; use route::Handler;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden}; use httpcodes::{HTTPOk, HTTPNotFound};
/// Static files handling /// Static files handling
/// ///
@ -149,26 +149,10 @@ impl<S> Handler<S> for StaticFiles {
// full filepath // full filepath
let idx = if filepath.starts_with('/') { 1 } else { 0 }; let idx = if filepath.starts_with('/') { 1 } else { 0 };
let filename = match self.directory.join(&filepath[idx..]).canonicalize() { let filename = 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),
}
};
if filename.is_dir() { if filename.is_dir() {
match self.index( self.index(&req.path()[..req.prefix_len()], &filepath[idx..], &filename)
&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),
}
}
} else { } else {
let mut resp = HTTPOk.build(); let mut resp = HTTPOk.build();
if let Some(ext) = filename.extension() { if let Some(ext) = filename.extension() {