1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 16:16:40 +00: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
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 {}

View file

@ -172,7 +172,7 @@ macro_rules! FROM_STR {
type Err = BadRequest<<$type as FromStr>::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 httprequest::HttpRequest;
use httpresponse::HttpResponse;
use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden};
use httpcodes::{HTTPOk, HTTPNotFound};
/// Static files handling
///
@ -149,26 +149,10 @@ impl<S> Handler<S> 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() {