docs(examples): replace unwraps with expects explaining why (#4001)

Just avoid unwrap as much as possible. Use expect to assert that it will always succeed.

Closes #3984
This commit is contained in:
Dongpo Liu
2026-01-12 21:28:42 +01:00
committed by GitHub
parent 79d40f3693
commit 0f0b6ed3ac
5 changed files with 14 additions and 14 deletions

View File

@@ -52,7 +52,7 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("constant status won't error"));
};
let number = if let Some(n) = params.get("number") {
if let Ok(v) = n.parse::<f64>() {
@@ -61,13 +61,13 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(NOTNUMERIC))
.unwrap());
.expect("constant status won't error"));
}
} else {
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("constant status won't error"));
};
// Render the response. This will often involve
@@ -86,7 +86,7 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("constant status won't error"));
};
let params = form_urlencoded::parse(query.as_bytes())
.into_owned()
@@ -97,7 +97,7 @@ async fn param_example(
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(full(MISSING))
.unwrap());
.expect("constant status won't error"));
};
let body = format!("You requested {}", page);
Ok(Response::new(full(body)))
@@ -105,7 +105,7 @@ async fn param_example(
_ => Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(empty())
.unwrap()),
.expect("constant status won't error")),
}
}

View File

@@ -61,7 +61,7 @@ fn not_found() -> Response<BoxBody<Bytes, std::io::Error>> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Full::new(NOTFOUND.into()).map_err(|e| match e {}).boxed())
.unwrap()
.expect("constant status won't error")
}
async fn simple_file_send(filename: &str) -> Result<Response<BoxBody<Bytes, std::io::Error>>> {
@@ -85,7 +85,7 @@ async fn simple_file_send(filename: &str) -> Result<Response<BoxBody<Bytes, std:
let response = Response::builder()
.status(StatusCode::OK)
.body(boxed_body)
.unwrap();
.expect("constant status won't error");
Ok(response)
}

View File

@@ -51,7 +51,7 @@ impl Service<Request<IncomingBody>> for Svc {
fn call(&self, req: Request<IncomingBody>) -> Self::Future {
fn mk_response(s: String) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::builder().body(Full::new(Bytes::from(s))).unwrap())
Ok(Response::new(Full::new(Bytes::from(s))))
}
if req.uri().path() != "/favicon.ico" {

View File

@@ -100,7 +100,7 @@ async fn client_upgrade_request(addr: SocketAddr) -> Result<()> {
.uri(format!("http://{}/", addr))
.header(UPGRADE, "foobar")
.body(Empty::<Bytes>::new())
.unwrap();
.expect("uri/header parse won't error");
let stream = TcpStream::connect(addr).await?;
let io = TokioIo::new(stream);

View File

@@ -29,7 +29,7 @@ async fn client_request_response() -> Result<Response<BoxBody>> {
.uri(URL)
.header(header::CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(POST_DATA)))
.unwrap();
.expect("uri/header parse from constants won't error");
let host = req.uri().host().expect("uri has no host");
let port = req.uri().port_u16().expect("uri has no port");
@@ -73,11 +73,11 @@ async fn api_get_response() -> Result<Response<BoxBody>> {
Ok(json) => Response::builder()
.header(header::CONTENT_TYPE, "application/json")
.body(full(json))
.unwrap(),
.expect("header parse from constant won't error"),
Err(_) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(full(INTERNAL_SERVER_ERROR))
.unwrap(),
.expect("constant status won't error"),
};
Ok(res)
}
@@ -93,7 +93,7 @@ async fn response_examples(req: Request<IncomingBody>) -> Result<Response<BoxBod
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(full(NOTFOUND))
.unwrap())
.expect("constant status won't error"))
}
}
}