Skip to content

Commit

Permalink
Docs fixes (LukeMathWalker#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker authored Mar 24, 2024
1 parent d3542f8 commit 9569b05
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion doc_examples/quickstart/06-register.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
```rust title="app/src/blueprint.rs" hl_lines="4"
```rust title="app/src/blueprint.rs" hl_lines="6"
// [...]
use pavex::f;
// [...]
pub fn blueprint() -> Blueprint {
// [...]
Expand Down
2 changes: 1 addition & 1 deletion doc_examples/quickstart/08-error_handler.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// [...]
pub fn invalid_user_agent(_e: &ToStrError) -> Response {
Response::bad_request()
.set_typed_body("The `User-Agent` header value must be a valid UTF-8 string")
.set_typed_body("The `User-Agent` header value can only use ASCII printable characters.")
}
```
2 changes: 1 addition & 1 deletion doc_examples/quickstart/08.patch
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ index bb1f65b..78360bd 100644
+
+pub fn invalid_user_agent(_e: &ToStrError) -> Response {
+ Response::bad_request()
+ .set_typed_body("The `User-Agent` header value must be a valid UTF-8 string")
+ .set_typed_body("The `User-Agent` header value can only use ASCII printable characters.")
+}
4 changes: 2 additions & 2 deletions doc_examples/quickstart/tutorial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ steps:
ranges: [ "0..2", "10..23" ]
- name: "register"
source_path: "app/src/blueprint.rs"
ranges: [ "7..8", "12..13", "16..17" ]
hl_lines: [ 4 ]
ranges: [ "2..3", "7..8", "12..13", "16..17" ]
hl_lines: [ 6 ]
commands:
- command: "cargo px c"
expected_outcome: "success"
Expand Down
4 changes: 2 additions & 2 deletions docs/getting_started/quickstart/error_handling.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Error handling

In `UserAgent::extract` you're only handling the happy path:
the method panics if the `User-Agent` header is not valid UTF-8.
the method panics if the `User-Agent` header contains characters that are not [ASCII printable](https://www.ascii-code.com/characters/printable-characters).
Panicking for bad user input is poor behavior: you should handle the issue gracefully and return an error instead.

Let's change the signature of `UserAgent::extract` to return a `Result` instead:

--8<-- "doc_examples/quickstart/07-extract.snap"

1. `ToStrError` is the error type returned by `to_str` when the header value is not valid UTF-8.
1. `ToStrError` is the error type by `HeaderValue`'s `to_str` when there is a non-printable ASCII character in the header.

## All errors must be handled

Expand Down

0 comments on commit 9569b05

Please sign in to comment.