Start with the idea
A useful error says what operation failed and retains the original cause. Add context at abstraction boundaries rather than replacing specific information with one vague message.
An error should carry enough information for someone at the boundary to understand what failed. text.parse::<u16>() can report a parsing failure, but a caller may need to know that this text was supposed to be a port. The example's map_err adds the offending value and the operation while preserving the underlying cause in the formatted message. At a higher layer, you might add which configuration file or command supplied that value.
The useful question is where to add context.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.