API and Numeric Boundaries · 14 min · free with an account

Error Types at API Boundaries

Lesson preview · about 30%

An error type should preserve the distinctions the caller needs to handle.

Core idea

Choose error structure from caller decisions; absence, failure and defaulting are different policies.

Start with the idea

A missing optional value and an invalid supplied value describe different outcomes. Result<Option<T>, E> can express success with absence, success with a value, or failure. The example converts Option<Result<u16, ParseIntError>> using transpose, preserving all three cases without manufacturing a default port.

At an application boundary you may add contextual information such as a safe field name or a public operation ID. Preserve a useful source error when appropriate and avoid depending on human-readable error strings for control flow. A library can expose typed error cases, while an executable may aggregate errors for reporting.

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 754 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep