File tree Expand file tree Collapse file tree
courses/rust_essentials/200_error_handling Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,31 +6,22 @@ Introduction
66Topics Covered
77----------------
88
9- * **Panics **
9+ * **Errors **
1010
11- * Unrecoverable errors
12-
13- * **Recoverable Errors **
14-
15- * :rust: `Result `
11+ * Unrecoverable errors - :rust: `panic! `
12+ * Recoverable Errors - :rust: `Result `
1613
1714* **Try Operator **
1815
1916 * Error propagation
20-
21- * **Try Conversions **
22-
2317 * Converting error traits
2418
2519* **Error Trait **
2620
2721 * Defining error implementations
2822
29- * **Creating Errors Through ** :rust: `derive `
30-
31- * :rust: `thiserror `
32-
3323* **Simplified Error Handling **
3424
25+ * Creating Errors Through :rust: `derive ` - :rust: `thiserror `
3526 * :rust: `anyhow `
3627
Original file line number Diff line number Diff line change @@ -16,13 +16,9 @@ Unrecoverable Error
1616
1717 * Failed :rust: `assert! ` or :rust: `debug_assert! `
1818
19- * You can also manually determine you're in trouble
19+ * Call :rust: ` panic! ` when logic indicates unrecoverable error
2020
21- * Control flow gets somewhere it shouldn't
22-
23- * :rust: `panic! ` is the call used in these situations
24-
25- * Signals unrecoverable error - triggering thread shut-down
21+ * Triggers thread shut-down
2622 * Configurable - either unwind stack or abort
2723
2824------------------------------
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ Recoverable Error
2424
2525 .. note ::
2626
27- You need to handle both good and bad variants!
27+ You need to handle both variants!
2828
2929------------------
3030Handling Results
@@ -84,7 +84,9 @@ Propagating Errors
8484
8585* Shortcut - :rust: `? ` operator makes propagation concise
8686
87- * Called the **Try Operator ** - see next chapter for details
87+ * Called the **Try Operator **
88+
89+ * See next chapter for details
8890
8991.. container :: latex_environment footnotesize
9092
Original file line number Diff line number Diff line change @@ -9,10 +9,11 @@ Try Operator
99* Replaces repetitive match handling
1010* Keeps code focused on the "happy path"
1111
12- * How it works
12+ * Automatically decodes :rust: ` Result `
1313
1414 * :rust: `Ok(value) ` - **unwrap ** value and continue execution
1515 * :rust: `Err(E) ` - returns early
16+ * Works similarly for :rust: `Option `
1617
1718* Limitation
1819
@@ -87,6 +88,6 @@ Returning "Result" from "main"
8788 Ok(())
8889 }
8990
90- .. latex_environment :: tiny
91+ .. container :: latex_environment scriptsize
9192
92- `` Error: Os { code: 2, kind: NotFound, message: "No such file or directory" } ` `
93+ :error: ` Error: Os { code: 2, kind: NotFound, message: "No such file or directory" } `
Original file line number Diff line number Diff line change @@ -13,10 +13,7 @@ Automatic Error Type Conversion
1313
1414.. code :: rust
1515
16- enum Reason {
17- TooYoung,
18- TooOld,
19- }
16+ enum Reason { TooYoung, TooOld, }
2017
2118 // Error type is 'Reason'
2219 fn check_age(age: i32) -> Result<i32, Reason> {
@@ -28,11 +25,12 @@ Automatic Error Type Conversion
2825 // '?' sees 'Reason', knows the return type is 'String',
2926 // and converts it behind the scenes.
3027 check_age(10)?;
31-
3228 Ok(())
3329 }
3430
35- * Return error type must implement :rust: `From ` trait for source error type
31+ .. note ::
32+
33+ Return error type must implement :rust: `From ` trait for source error type
3634
3735 * Compiler verifies a valid path exists to convert the error
3836 * If not, it throws a *trait bound not satisfied * error
Original file line number Diff line number Diff line change @@ -50,6 +50,13 @@ Trait Definition
5050 * Allows you to peel back layers of error to find root cause
5151 * E.g a "network error" caused by a "timeout"
5252
53+ .. note ::
54+
55+ :rust: `Source ` returns an :rust: `Option ` type where :rust: `Error ` is
56+
57+ * :rust: `dyn ` - trait object (not a fixed-size type)
58+ * :rust: `'static ` - has no temporary references
59+
5360------------------------
5461Implementing the Trait
5562------------------------
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ One Type to Rule Them All
5050 use anyhow::Result;
5151
5252 fn run_app() -> Result<()> {
53- let config = read_config()?; // Could be 'io::Error'
53+ let config = read_config()?; // Could be 'io::Error'
5454 let data = parse_data(config)?; // Could be 'ParseError'
5555 Ok(())
5656 }
You can’t perform that action at this time.
0 commit comments