support custom not found endpoint
Feature Request
Currently, Tide always returns 404 with an empty body when there is no route matched.
https://github.com/rustasync/tide/blob/9a9b10a6fa7ad3aebbebd7a2404ac4ddc4c17ab7/src/router/core.rs#L71
But, we often expect to return some message to let our user know what happened.
Possible Implementation
To archive this goal, we have some options under current implementation:
- set a "catch-all" route to make "not found" fallback to that handler.
- implement a middleware to handle this situation, but how to judge things exactly right(i.e. 404 with an empty body may not cause by route missing)
- support register an
Endpointwhich should be called when route not found.
@tzilist mentioned (#10) that we can make something like actix-web does, but actix-web ErrorHandlers associated handler with a status code. While seems Tide do not have trait ResponseError, which can provide detailed context to handle errors. For example, when returning 400, the concrete situation may be the request body can not be deserialized, or some fields in the body did not pass the verification, we often have different biz status code and error message for such scenarios, which is common needs when implementing RESTful API, with ResponseError, we can distinguish what happened exactly. Maybe we should consider a similar field in Context<_>.
Ref #10 #62
This was added in #64, but removed during the latest round of refactors. We've discussed this during triage and agreed we should restore this API.
Any advance on this?
I tried with:
use tide::Request;
#[async_std::main]
async fn main() -> tide::Result<()> {
let mut app = tide::new();
app.at("*").all(fallback);
app.listen("127.0.0.1:8080").await?;
Ok(())
}
async fn fallback(req: Request<()>) -> tide::Result {
Ok(format!("Hello `{}`!", req.url()).into())
}
But fails to match the root /