Skip to content

Commit b7ddd09

Browse files
committed
address review feedback
Adjust error text and naming to conform with best practices. Use `map_err()` instead of `or()`. Wrap lower-level errors instead of ignoring their details. Also, don't "cheat" by bypassing the `new()` function in tests. Fix a dangling reference in the try_from_into hints.
1 parent 68d3ac5 commit b7ddd09

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

exercises/error_handling/errors5.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl PositiveNonzeroInteger {
4343
impl fmt::Display for CreationError {
4444
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4545
let description = match *self {
46-
CreationError::Negative => "Number is negative",
47-
CreationError::Zero => "Number is zero",
46+
CreationError::Negative => "number is negative",
47+
CreationError::Zero => "number is zero",
4848
};
4949
f.write_str(description)
5050
}

exercises/error_handling/errors6.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@
1010

1111
// I AM NOT DONE
1212

13+
use std::num::ParseIntError;
14+
1315
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
1416
#[derive(PartialEq, Debug)]
1517
enum ParsePosNonzeroError {
16-
CreationError,
17-
ParseIntError
18+
Creation(CreationError),
19+
ParseInt(ParseIntError)
20+
}
21+
22+
impl ParsePosNonzeroError {
23+
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
24+
ParsePosNonzeroError::Creation(err)
25+
}
26+
// TODO: add another error conversion function here.
1827
}
1928

2029
fn parse_pos_nonzero(s: &str)
@@ -24,7 +33,7 @@ fn parse_pos_nonzero(s: &str)
2433
// when `parse()` returns an error.
2534
let x: i64 = s.parse().unwrap();
2635
PositiveNonzeroInteger::new(x)
27-
.or(Err(ParsePosNonzeroError::CreationError))
36+
.map_err(ParsePosNonzeroError::from_creation)
2837
}
2938

3039
// Don't change anything below this line.
@@ -54,33 +63,33 @@ mod test {
5463

5564
#[test]
5665
fn test_parse_error() {
57-
assert_eq!(
66+
// We can't construct a ParseIntError, so we have to pattern match.
67+
assert!(matches!(
5868
parse_pos_nonzero("not a number"),
59-
Err(ParsePosNonzeroError::ParseIntError)
60-
);
69+
Err(ParsePosNonzeroError::ParseInt(_))
70+
));
6171
}
6272

6373
#[test]
6474
fn test_negative() {
6575
assert_eq!(
6676
parse_pos_nonzero("-555"),
67-
Err(ParsePosNonzeroError::CreationError)
77+
Err(ParsePosNonzeroError::Creation(CreationError::Negative))
6878
);
6979
}
7080

7181
#[test]
7282
fn test_zero() {
7383
assert_eq!(
7484
parse_pos_nonzero("0"),
75-
Err(ParsePosNonzeroError::CreationError)
85+
Err(ParsePosNonzeroError::Creation(CreationError::Zero))
7686
);
7787
}
7888

7989
#[test]
8090
fn test_positive() {
81-
assert_eq!(
82-
parse_pos_nonzero("42"),
83-
Ok(PositiveNonzeroInteger(42))
84-
);
91+
let x = PositiveNonzeroInteger::new(42);
92+
assert!(x.is_ok());
93+
assert_eq!(parse_pos_nonzero("42"), Ok(x.unwrap()));
8594
}
8695
}

info.toml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,19 @@ path = "exercises/error_handling/errors6.rs"
532532
mode = "test"
533533
hint = """
534534
This exercise uses a completed version of `PositiveNonzeroInteger` from
535-
the errors4.
535+
errors4.
536536
537-
Below the TODO line, there is an example of using the `.or()` method
538-
on a `Result` to transform one type of error into another. Try using
539-
something similar on the `Result` from `parse()`. You might use the `?`
540-
operator to return early from the function, or you might use a `match`
541-
expression, or maybe there's another way!
537+
Below the line that TODO asks you to change, there is an example of using
538+
the `map_err()` method on a `Result` to transform one type of error into
539+
another. Try using something similar on the `Result` from `parse()`. You
540+
might use the `?` operator to return early from the function, or you might
541+
use a `match` expression, or maybe there's another way!
542542
543-
Read more about `.or()` in the `std::result` documentation:
544-
https://doc.rust-lang.org/std/result/enum.Result.html#method.or"""
543+
You can create another function inside `impl ParsePosNonzeroError` to use
544+
with `map_err()`.
545+
546+
Read more about `map_err()` in the `std::result` documentation:
547+
https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err"""
545548

546549
# Generics
547550

@@ -927,7 +930,7 @@ hint = """
927930
Follow the steps provided right before the `TryFrom` implementation.
928931
You can also use the example at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
929932
930-
You might want to look back at the exercise errorsn (or its hints) to remind
933+
You might want to look back at the exercise errors5 (or its hints) to remind
931934
yourself about how `Box<dyn Error>` works.
932935
933936
If you're trying to return a string as an error, note that neither `str`

0 commit comments

Comments
 (0)