Skip to content

Commit a896d96

Browse files
committed
Finish ok-wrapping examples
1 parent 05cab7f commit a896d96

File tree

7 files changed

+36
-27
lines changed

7 files changed

+36
-27
lines changed

src/endpoint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::{Middleware, Request, Response};
2424
/// A simple endpoint that is invoked on a `GET` request and returns a `String`:
2525
///
2626
/// ```no_run
27-
/// async fn hello(_req: tide::Request<()>) -> String {
28-
/// String::from("hello")
27+
/// async fn hello(_req: tide::Request<()>) -> tide::Result<String> {
28+
/// Ok(String::from("hello"))
2929
/// }
3030
///
3131
/// fn main() {
@@ -38,8 +38,8 @@ use crate::{Middleware, Request, Response};
3838
///
3939
/// ```no_run
4040
/// # use core::future::Future;
41-
/// fn hello(_req: tide::Request<()>) -> impl Future<Output = String> {
42-
/// futures::future::ready(String::from("hello"))
41+
/// fn hello(_req: tide::Request<()>) -> impl Future<Output = tide::Result<String>> {
42+
/// futures::future::ready(Ok(String::from("hello")))
4343
/// }
4444
///
4545
/// fn main() {

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! # fn main() -> Result<(), std::io::Error> { block_on(async {
2121
//! #
2222
//! let mut app = tide::new();
23-
//! app.at("/").get(|_| async move { "Hello, world!" });
23+
//! app.at("/").get(|_| async move { Ok("Hello, world!") });
2424
//! app.listen("127.0.0.1:8080").await?;
2525
//! #
2626
//! # Ok(()) }) }
@@ -32,7 +32,7 @@
3232
//! # fn main() -> Result<(), std::io::Error> { block_on(async {
3333
//! #
3434
//! let mut app = tide::new();
35-
//! app.at("/").get(|req| async move { req });
35+
//! app.at("/").get(|req| async move { Ok(req) });
3636
//! app.listen("127.0.0.1:8080").await?;
3737
//! #
3838
//! # Ok(()) }) }
@@ -48,10 +48,10 @@
4848
//!
4949
//! let mut app = tide::new();
5050
//! app.at("/").get(|mut req: tide::Request<()>| async move {
51-
//! let mut counter: Counter = req.body_json().await.unwrap();
51+
//! let mut counter: Counter = req.body_json().await?;
5252
//! println!("count is {}", counter.count);
5353
//! counter.count += 1;
54-
//! tide::Response::new(http_types::StatusCode::Ok).body_json(&counter).unwrap()
54+
//! Ok(tide::Response::new(tide::http::StatusCode::Ok).body_json(&counter)?)
5555
//! });
5656
//! app.listen("127.0.0.1:8080").await?;
5757
//! #
@@ -150,7 +150,7 @@
150150
//! #[async_std::main]
151151
//! async fn main() -> Result<(), std::io::Error> {
152152
//! let mut app = tide::new();
153-
//! app.at("/").get(|req: Request<()>| async move { req.bark() });
153+
//! app.at("/").get(|req: Request<()>| async move { Ok(req.bark()) });
154154
//! app.listen("127.0.0.1:8080").await
155155
//! }
156156
//! ```
@@ -210,7 +210,7 @@ pub use http_types as http;
210210
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
211211
/// #
212212
/// let mut app = tide::new();
213-
/// app.at("/").get(|_| async move { "Hello, world!" });
213+
/// app.at("/").get(|_| async move { Ok("Hello, world!") });
214214
/// app.listen("127.0.0.1:8080").await?;
215215
/// #
216216
/// # Ok(()) }) }
@@ -244,7 +244,7 @@ pub fn new() -> server::Server<()> {
244244
/// // Initialize the application with state.
245245
/// let mut app = tide::with_state(state);
246246
/// app.at("/").get(|req: Request<State>| async move {
247-
/// format!("Hello, {}!", &req.state().name)
247+
/// Ok(format!("Hello, {}!", &req.state().name))
248248
/// });
249249
/// app.listen("127.0.0.1:8080").await?;
250250
/// #

src/middleware/cookies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use std::sync::{Arc, RwLock};
1414
///
1515
/// ```
1616
/// let mut app = tide::Server::new();
17-
/// app.at("/get").get(|cx: tide::Request<()>| async move { cx.cookie("testCookie").unwrap().value().to_string() });
17+
/// app.at("/get").get(|cx: tide::Request<()>| async move { Ok(cx.cookie("testCookie").unwrap().value().to_string()) });
1818
/// app.at("/set").get(|_| async {
1919
/// let mut res = tide::Response::new(http_types::StatusCode::Ok);
2020
/// res.set_cookie(cookie::Cookie::new("testCookie", "NewCookieValue"));
21-
/// res
21+
/// Ok(res)
2222
/// });
2323
/// ```
2424
#[derive(Debug, Clone, Default)]

src/redirect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{Endpoint, Request, Response};
1414
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
1515
/// #
1616
/// let mut app = tide::new();
17-
/// app.at("/").get(|_| async move { "meow" });
17+
/// app.at("/").get(|_| async move { Ok("meow") });
1818
/// app.at("/nori").get(tide::redirect("/"));
1919
/// app.listen("127.0.0.1:8080").await?;
2020
/// #

src/request.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<State> Request<State> {
5454
/// let mut app = tide::new();
5555
/// app.at("/").get(|req: Request<()>| async move {
5656
/// assert_eq!(req.method(), http_types::Method::Get);
57-
/// ""
57+
/// Ok("")
5858
/// });
5959
/// app.listen("127.0.0.1:8080").await?;
6060
/// #
@@ -76,8 +76,8 @@ impl<State> Request<State> {
7676
///
7777
/// let mut app = tide::new();
7878
/// app.at("/").get(|req: Request<()>| async move {
79-
/// assert_eq!(req.uri(), &"/".parse::<tide::http_types::Url>().unwrap());
80-
/// ""
79+
/// assert_eq!(req.uri(), &"/".parse::<tide::http::Url>().unwrap());
80+
/// Ok("")
8181
/// });
8282
/// app.listen("127.0.0.1:8080").await?;
8383
/// #
@@ -99,8 +99,8 @@ impl<State> Request<State> {
9999
///
100100
/// let mut app = tide::new();
101101
/// app.at("/").get(|req: Request<()>| async move {
102-
/// assert_eq!(req.version(), Some(tide::http_types::Version::Http1_1));
103-
/// ""
102+
/// assert_eq!(req.version(), Some(http_types::Version::Http1_1));
103+
/// Ok("")
104104
/// });
105105
/// app.listen("127.0.0.1:8080").await?;
106106
/// #
@@ -123,7 +123,7 @@ impl<State> Request<State> {
123123
/// let mut app = tide::new();
124124
/// app.at("/").get(|req: Request<()>| async move {
125125
/// assert_eq!(req.header(&"X-Forwarded-For".parse().unwrap()), Some(&vec!["127.0.0.1".parse().unwrap()]));
126-
/// ""
126+
/// Ok("")
127127
/// });
128128
/// app.listen("127.0.0.1:8080").await?;
129129
/// #
@@ -205,7 +205,7 @@ impl<State> Request<State> {
205205
/// let mut app = tide::new();
206206
/// app.at("/").get(|mut req: Request<()>| async move {
207207
/// let _body: Vec<u8> = req.body_bytes().await.unwrap();
208-
/// ""
208+
/// Ok("")
209209
/// });
210210
/// app.listen("127.0.0.1:8080").await?;
211211
/// #
@@ -240,7 +240,7 @@ impl<State> Request<State> {
240240
/// let mut app = tide::new();
241241
/// app.at("/").get(|mut req: Request<()>| async move {
242242
/// let _body: String = req.body_string().await.unwrap();
243-
/// ""
243+
/// Ok("")
244244
/// });
245245
/// app.listen("127.0.0.1:8080").await?;
246246
/// #

src/server/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl Server<()> {
151151
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
152152
/// #
153153
/// let mut app = tide::new();
154-
/// app.at("/").get(|_| async move { "Hello, world!" });
154+
/// app.at("/").get(|_| async move { Ok("Hello, world!") });
155155
/// app.listen("127.0.0.1:8080").await?;
156156
/// #
157157
/// # Ok(()) }) }
@@ -193,7 +193,7 @@ impl<State: Send + Sync + 'static> Server<State> {
193193
/// // Initialize the application with state.
194194
/// let mut app = tide::with_state(state);
195195
/// app.at("/").get(|req: Request<State>| async move {
196-
/// format!("Hello, {}!", &req.state().name)
196+
/// Ok(format!("Hello, {}!", &req.state().name))
197197
/// });
198198
/// app.listen("127.0.0.1:8080").await?;
199199
/// #
@@ -219,7 +219,7 @@ impl<State: Send + Sync + 'static> Server<State> {
219219
///
220220
/// ```rust,no_run
221221
/// # let mut app = tide::Server::new();
222-
/// app.at("/").get(|_| async move {"Hello, world!"});
222+
/// app.at("/").get(|_| async move { Ok("Hello, world!") });
223223
/// ```
224224
///
225225
/// A path is comprised of zero or many segments, i.e. non-empty strings

tests/querystring.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ async fn handler(cx: Request<()>) -> tide::Result<Response> {
2020
let p = cx.query::<Params>();
2121
match p {
2222
Ok(params) => Ok(params.msg.into_response()),
23-
Err(error) => Ok(Response::new(error.status())),
23+
Err(error) => Ok(err_to_res(error)),
2424
}
2525
}
2626

2727
async fn optional_handler(cx: Request<()>) -> tide::Result<Response> {
2828
let p = cx.query::<OptionalParams>();
2929
match p {
3030
Ok(_) => Ok(Response::new(StatusCode::Ok)),
31-
Err(error) => Ok(Response::new(error.status())),
31+
Err(error) => Ok(err_to_res(error)),
3232
}
3333
}
3434

@@ -94,3 +94,12 @@ fn empty_query_string_for_struct_with_no_required_fields() {
9494
let res = server.simulate(req).unwrap();
9595
assert_eq!(res.status(), StatusCode::Ok);
9696
}
97+
98+
fn err_to_res(err: http_types::Error) -> crate::Response {
99+
Response::new(err.status())
100+
.set_header(
101+
http_types::headers::CONTENT_TYPE,
102+
"text/plain; charset=utf-8",
103+
)
104+
.body_string(err.to_string())
105+
}

0 commit comments

Comments
 (0)