-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error-handling: split thiserror into its own slide
`thiserror` is best understood as a way to eliminate boilerplate on the patterns we've already seen, and then we can show it in conjunction with `anyhow` subsequently.
- Loading branch information
1 parent
3269cb9
commit a1b377d
Showing
4 changed files
with
66 additions
and
23 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
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,51 @@ | ||
--- | ||
minutes: 5 | ||
--- | ||
|
||
# `thiserror` | ||
|
||
The [`thiserror`](https://docs.rs/thiserror/) crate provides macros to help | ||
avoid boilerplate when defining error types. It provides derive macros that | ||
assist in implementing `From<T>`, `Display`, and the `Error` trait. | ||
|
||
```rust,editable,compile_fail | ||
use std::fs; | ||
use std::io::Read; | ||
use thiserror::Error; | ||
#[derive(Error)] | ||
enum ReadUsernameError { | ||
#[error("I/O error: {0}")] | ||
IoError(#[from] io::Error), | ||
#[error("Found no username in {0}")] | ||
EmptyUsername(String), | ||
} | ||
fn read_username(path: &str) -> Result<String, ReadUsernameError> { | ||
let mut username = String::with_capacity(100); | ||
File::open(path)?.read_to_string(&mut username)?; | ||
if username.is_empty() { | ||
return Err(ReadUsernameError::EmptyUsername(String::from(path))); | ||
} | ||
Ok(username) | ||
} | ||
fn main() { | ||
//fs::write("config.dat", "").unwrap(); | ||
match read_username("config.dat") { | ||
Ok(username) => println!("Username: {username}"), | ||
Err(err) => println!("Error: {err:?}"), | ||
} | ||
} | ||
``` | ||
|
||
<details> | ||
|
||
- The `Error` derive macro is provided by `thiserror`, and has lots of useful | ||
attributes to help define error types in a compact way. | ||
- The message from `#[error]` is used to derive the `Display` trait. | ||
- Note that the (`thiserror::`)`Error` derive macro, while it has the effect of | ||
implementing the (`std::error::`)`Error` trait, is not the same this; traits | ||
and macros do not share a namespace. | ||
|
||
</details> |
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