Handling Result and errors in a Rust web handler
WHAT IT TESTS: Result-based error handling in web handlers. OUTLINE: handler returns Result, the ? operator early-returns errors, a custom error type implements IntoResponse to map to 500, success returns 200 with data.
WHAT IT TESTS: idiomatic Rust error handling wired into a framework like axum. ANSWER OUTLINE: the handler returns Result<T, AppError>. The ? operator propagates a query error early, converting it via From into your AppError. AppError implements IntoResponse, mapping variants to status codes so a DB failure becomes 500 while success returns 200 with the serialized data. This keeps handlers linear and avoids panics. RED FLAG: calling .unwrap or .expect on the Result, which panics and may crash the worker instead of returning a clean 500.
Read the original → interview
- #rust
- #error-handling
- #axum
- #web
- #result
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.