Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Log errors #59

Merged
merged 2 commits into from
Jun 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,12 @@ impl Logger {
struct StartTime;
impl Key for StartTime { type Value = u64; }

impl BeforeMiddleware for Logger {
fn before(&self, req: &mut Request) -> IronResult<()> {
impl Logger {
fn initialise(&self, req: &mut Request) {
req.extensions.insert::<StartTime>(time::precise_time_ns());
Ok(())
}

fn catch(&self, _: &mut Request, err: IronError) -> IronResult<()> {
Err(err)
}
}

impl AfterMiddleware for Logger {
fn after(&self, req: &mut Request, res: Response) -> IronResult<Response> {
fn log(&self, req: &mut Request, res: &Response) -> IronResult<()> {
let exit_time = time::precise_time_ns();
let entry_time = *req.extensions.get::<StartTime>().unwrap();

Expand All @@ -86,7 +79,7 @@ impl AfterMiddleware for Logger {
match unit.color {
ConstantColor(Some(color)) => { try!(t.fg(color)); }
ConstantColor(None) => (),
FunctionColor(f) => match f(req, &res) {
FunctionColor(f) => match f(req, res) {
Some(color) => { try!(t.fg(color)); }
None => ()
}
Expand All @@ -98,7 +91,7 @@ impl AfterMiddleware for Logger {
}
}
FunctionAttrs(f) => {
for &attr in f(req, &res).iter() {
for &attr in f(req, res).iter() {
try!(t.attr(attr));
}
}
Expand All @@ -122,10 +115,30 @@ impl AfterMiddleware for Logger {
};
}

Ok(())
}
}

impl BeforeMiddleware for Logger {
fn before(&self, req: &mut Request) -> IronResult<()> {
self.initialise(req);
Ok(())
}

fn catch(&self, req: &mut Request, err: IronError) -> IronResult<()> {
self.initialise(req);
Err(err)
}
}

impl AfterMiddleware for Logger {
fn after(&self, req: &mut Request, res: Response) -> IronResult<Response> {
try!(self.log(req, &res));
Ok(res)
}

fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
fn catch(&self, req: &mut Request, err: IronError) -> IronResult<Response> {
try!(self.log(req, &err.response));
Err(err)
}
}
Expand Down