Result type and the ? operator. Errors are automatically converted to appropriate HTTP responses, making error handling clean and consistent throughout your application.
The Response Type
Kit’s controller handlers returnResponse, which is an alias for Result<HttpResponse, HttpResponse>:
- Using the
?operator for automatic error conversion - Returning successful responses with
Ok(HttpResponse::...) - Returning error responses with
Err(HttpResponse::...)
Quick Error Handling with ?
The most common pattern is using the ? operator, which automatically converts errors to HTTP responses:
Error Types
AppError
AppError is a simple wrapper for creating inline/ad-hoc errors with custom status codes:
AppError Helper Methods
| Method | Status Code | Description |
|---|---|---|
AppError::new(msg) | 500 | Internal Server Error |
AppError::not_found(msg) | 404 | Resource Not Found |
AppError::bad_request(msg) | 400 | Bad Request |
AppError::unauthorized(msg) | 401 | Unauthorized |
AppError::forbidden(msg) | 403 | Forbidden |
AppError::unprocessable(msg) | 422 | Unprocessable Entity |
AppError::conflict(msg) | 409 | Conflict |
FrameworkError
FrameworkError is Kit’s comprehensive error enum that handles all framework-level errors:
FrameworkError Factory Methods
| Method | Status Code | Use Case |
|---|---|---|
FrameworkError::service_not_found::<T>() | 500 | DI container resolution failed |
FrameworkError::param(name) | 400 | Missing route parameter |
FrameworkError::validation(field, msg) | 422 | Validation failed |
FrameworkError::database(msg) | 500 | Database operation failed |
FrameworkError::internal(msg) | 500 | Generic internal error |
FrameworkError::domain(msg, status) | Custom | Domain-specific error |
Automatic Error Conversion
Kit automatically converts common error types toFrameworkError:
Database Errors
SeaORM’sDbErr converts automatically:
Parameter Errors
Route parameter extraction returns errors usable with?:
AppError to FrameworkError
AppError converts to FrameworkError::Domain:
Error Response Format
Errors are automatically converted to JSON responses:Parameter Error (400)
Validation Error (422)
Generic Error
Creating Custom Domain Errors
Generating Errors with CLI
The fastest way to create a custom domain error is using the Kit CLI:- Create
src/errors/user_not_found.rswith a domain error struct - Create or update
src/errors/mod.rsto export the new error
The #[domain_error] Macro
The #[domain_error] macro automatically implements all necessary traits for HTTP error handling:
- Derives
DebugandClone - Implements
Display,Error, andHttpErrortraits - Implements
From<T> for FrameworkErrorfor seamless?usage
? operator:
Using AppError
For simple, inline errors:Implementing HttpError Trait
For reusable domain errors, implement theHttpError trait:
Creating Error Enums
For complex applications, create dedicated error enums:Common Error Patterns
Early Returns with ?
Validation Errors
Resource Not Found
Chaining Operations
Conditional Errors
Error Handling in Actions
Actions can returnResult<T, FrameworkError> for clean error propagation:
Summary
| Feature | Usage |
|---|---|
| Generate error | kit make:error ErrorName |
| Domain error macro | #[domain_error(status = 404, message = "...")] |
| Quick error | AppError::new("message") |
| Not found | AppError::not_found("message") |
| Bad request | AppError::bad_request("message") |
| Unauthorized | AppError::unauthorized("message") |
| Forbidden | AppError::forbidden("message") |
| Validation | FrameworkError::validation("field", "message") |
| Custom status | AppError::new("msg").status(code) |
| Domain error | FrameworkError::domain("msg", status) |
| Convert to Response | .into() or ? operator |
| Custom error type | Implement HttpError trait |
| Error propagation | Use ? operator |
| File location | src/errors/ |