diff --git a/Cargo.toml b/Cargo.toml index 34cfa79cfc..72a583b4c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,3 +86,8 @@ path = "examples/chaining.rs" name = "form_data" path = "examples/form_data/form_data.rs" + +[[example]] + +name = "error_handling" +path = "examples/error_handling.rs" diff --git a/examples/error_handling.rs b/examples/error_handling.rs new file mode 100644 index 0000000000..1d1c67fb37 --- /dev/null +++ b/examples/error_handling.rs @@ -0,0 +1,77 @@ +#[macro_use] extern crate nickel; + +use nickel::{Nickel, Request, Response, MiddlewareResult, HttpRouter}; +use nickel::status::StatusCode; +use std::{error,io, fmt}; + +#[derive(Debug)] +pub enum AppError { + Io(io::Error), + Custom(String) +} + +impl fmt::Display for AppError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + AppError::Custom(ref msg) => write!(f, "Custom error: {}", msg), + AppError::Io(ref err) => write!(f, "IO error: {}", err) + } + } +} + +impl error::Error for AppError { + fn description(&self) -> &str { + match *self { + AppError::Custom(ref msg) => msg, + AppError::Io(ref err) => err.description() + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + AppError::Custom(_) => None, + AppError::Io(ref err) => Some(err) + } + } +} + +impl<'a> From<&'a AppError> for StatusCode { + fn from(err: &AppError) -> StatusCode { + match *err { + AppError::Custom(_) => StatusCode::BadRequest, + AppError::Io(_) => StatusCode::InternalServerError + } + } +} + +type AppResult = Result; + +fn will_fail() -> AppResult { + Err(AppError::Custom("uups".to_string())) +} + +fn will_work() -> AppResult { + Ok("foo".to_string()) +} + +fn foo_handler<'a, D>(_: &mut Request, mut res: Response<'a,D>) -> MiddlewareResult<'a, D> { + + let x: AppResult = (||Ok({ + try!(will_fail()); + try!(will_work()) + }))(); + + match x { + Ok(s) => res.send(s), + Err(ref err) => { + res.set(StatusCode::from(err)); + res.send(err.to_string()) + } + } +} + +fn main() { + let mut server = Nickel::new(); + server.get("foo/", foo_handler); + server.listen("127.0.0.1:6767"); +}