Skip to content

Commit 3c3b9ad

Browse files
committed
Rollup merge of rust-lang#32340 - Digipom:master, r=steveklabnik
Update of the book; Error handling, section on custom error types: we… … should also show the changes to the `cause` method. When I started creating my own error type, I found that we also have to update the cause method, otherwise we have a missing match branch. It would also be nice to elaborate on the relationship and difference between the description() and fmt() method, but that should be done by someone with more experience with them. :)
2 parents 0694d63 + 8f99ad2 commit 3c3b9ad

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/doc/book/error-handling.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,16 @@ impl Error for CliError {
20192019
CliError::NotFound => "not found",
20202020
}
20212021
}
2022+
2023+
fn cause(&self) -> Option<&error::Error> {
2024+
match *self {
2025+
CliError::Io(ref err) => Some(err),
2026+
CliError::Parse(ref err) => Some(err),
2027+
// Our custom error doesn't have an underlying cause, but we could
2028+
// modify it so that it does.
2029+
CliError::NotFound() => None,
2030+
}
2031+
}
20222032
}
20232033
```
20242034

0 commit comments

Comments
 (0)