@@ -11,15 +11,15 @@ pub trait Error: Debug + Display {
|
||||
}
|
||||
```
|
||||
|
||||
The `source` method is a way to access the **error cause**, if any.
|
||||
The `source` method is a way to access the **error cause**, if any.\
|
||||
Errors are often chained, meaning that one error is the cause of another: you have a high-level error (e.g.
|
||||
cannot connect to the database) that is caused by a lower-level error (e.g. can't resolve the database hostname).
|
||||
The `source` method allows you to "walk" the full chain of errors, often used when capturing error context in logs.
|
||||
|
||||
## Implementing `source`
|
||||
|
||||
The `Error` trait provides a default implementation that always returns `None` (i.e. no underlying cause). That's why
|
||||
you didn't have to care about `source` in the previous exercises.
|
||||
The `Error` trait provides a default implementation that always returns `None` (i.e. no underlying cause). That's why
|
||||
you didn't have to care about `source` in the previous exercises.\
|
||||
You can override this default implementation to provide a cause for your error type.
|
||||
|
||||
```rust
|
||||
@@ -48,14 +48,14 @@ We then override the `source` method to return this source when called.
|
||||
|
||||
## `&(dyn Error + 'static)`
|
||||
|
||||
What's this `&(dyn Error + 'static)` type?
|
||||
What's this `&(dyn Error + 'static)` type?\
|
||||
Let's unpack it:
|
||||
|
||||
- `dyn Error` is a **trait object**. It's a way to refer to any type that implements the `Error` trait.
|
||||
- `'static` is a special **lifetime specifier**.
|
||||
`'static` implies that the reference is valid for "as long as we need it", i.e. the entire program execution.
|
||||
|
||||
Combined: `&(dyn Error + 'static)` is a reference to a trait object that implements the `Error` trait
|
||||
Combined: `&(dyn Error + 'static)` is a reference to a trait object that implements the `Error` trait
|
||||
and is valid for the entire program execution.
|
||||
|
||||
Don't worry too much about either of these concepts for now. We'll cover them in more detail in future chapters.
|
||||
@@ -75,7 +75,7 @@ Don't worry too much about either of these concepts for now. We'll cover them in
|
||||
source: std::io::Error
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
- A field annotated with the `#[source]` attribute will automatically be used as the source of the error.
|
||||
```rust
|
||||
use thiserror::Error;
|
||||
@@ -88,8 +88,8 @@ Don't worry too much about either of these concepts for now. We'll cover them in
|
||||
inner: std::io::Error
|
||||
}
|
||||
}
|
||||
```
|
||||
- A field annotated with the `#[from]` attribute will automatically be used as the source of the error **and**
|
||||
```
|
||||
- A field annotated with the `#[from]` attribute will automatically be used as the source of the error **and**
|
||||
`thiserror` will automatically generate a `From` implementation to convert the annotated type into your error type.
|
||||
```rust
|
||||
use thiserror::Error;
|
||||
@@ -102,11 +102,11 @@ Don't worry too much about either of these concepts for now. We'll cover them in
|
||||
inner: std::io::Error
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
## The `?` operator
|
||||
|
||||
The `?` operator is a shorthand for propagating errors.
|
||||
The `?` operator is a shorthand for propagating errors.\
|
||||
When used in a function that returns a `Result`, it will return early with an error if the `Result` is `Err`.
|
||||
|
||||
For example:
|
||||
@@ -145,7 +145,7 @@ fn read_file() -> Result<String, std::io::Error> {
|
||||
}
|
||||
```
|
||||
|
||||
You can use the `?` operator to shorten your error handling code significantly.
|
||||
You can use the `?` operator to shorten your error handling code significantly.\
|
||||
In particular, the `?` operator will automatically convert the error type of the fallible operation into the error type
|
||||
of the function, if a conversion is possible (i.e. if there is a suitable `From` implementation)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user