-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples: add error handling example
Related to #549
- Loading branch information
1 parent
edac181
commit f2d1608
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use tide::{Request, Response, Result, StatusCode}; | ||
use tide::utils::After; | ||
|
||
#[async_std::main] | ||
async fn main() -> Result<()> { | ||
tide::log::start(); | ||
let mut app = tide::new(); | ||
|
||
app.middleware(After(|mut res: Response| async move { | ||
if let Some(err) = res.downcast_error::<url::ParseError>() { | ||
let msg = err.to_string().to_owned(); | ||
res.set_status(StatusCode::ImATeapot); | ||
res.set_body(format!("Teapot Status: {}", msg)); | ||
} | ||
Ok(res) | ||
})); | ||
|
||
app.at("/").get(|_req: Request<_>| async move { | ||
let path = url::Url::parse("")?; | ||
Ok(format!("Path is {}", path)) | ||
}); | ||
|
||
app.listen("127.0.0.1:8080").await?; | ||
|
||
Ok(()) | ||
} |