Closed lilydjwg closed 8 years ago
You should be able to pattern match on the error
field of IronError
. As you can see in router.rs, NoRoute
is placed into the error
field via IronError::new()
. Note though that this means NoRoute
will be Box
ed, which will need to be included in the pattern.
You'll have to use the downcast
or is
methods, available via the Error
trait.
@reem how should I use it? I tried but got the following error:
src/basic.rs:33:21: 33:36 error: no method named `is` found for type `error::Error + Send + 'static` in the current scope
src/basic.rs:33 if (*err.error).is::<NoRoute>() {
^~~~~~~~~~~~~~~
@lilydjwg have you checked out the custom_404.rs
example file?
impl AfterMiddleware for Custom404 {
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
println!("Hitting custom 404 middleware");
if let Some(_) = err.error.downcast::<NoRoute>() {
Ok(Response::with((status::NotFound, "Custom 404 response")))
} else {
Err(err)
}
}
}
@kellenfujimoto thanks, it works!
I want to write a middleware that provides custom error pages for the
NoRoute
error, but I didn't figure out how to know whether anIronError
is caused byNoRoute
.This is what I've got: