Skip to main content
Kit provides a comprehensive error handling system that integrates seamlessly with Rust’s 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 return Response, which is an alias for Result<HttpResponse, HttpResponse>:
This design enables:
  • 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

FrameworkError

FrameworkError is Kit’s comprehensive error enum that handles all framework-level errors:

FrameworkError Factory Methods

Automatic Error Conversion

Kit automatically converts common error types to FrameworkError:

Database Errors

SeaORM’s DbErr 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:
This command will:
  1. Create src/errors/user_not_found.rs with a domain error struct
  2. Create or update src/errors/mod.rs to export the new error

The #[domain_error] Macro

The #[domain_error] macro automatically implements all necessary traits for HTTP error handling:
  • Derives Debug and Clone
  • Implements Display, Error, and HttpError traits
  • Implements From<T> for FrameworkError for seamless ? usage
Use generated errors in controllers with the ? operator:

Using AppError

For simple, inline errors:

Implementing HttpError Trait

For reusable domain errors, implement the HttpError trait:

Creating Error Enums

For complex applications, create dedicated error enums:
Use in controllers:

Common Error Patterns

Early Returns with ?

Validation Errors

Resource Not Found

Chaining Operations

Conditional Errors

Error Handling in Actions

Actions can return Result<T, FrameworkError> for clean error propagation:
Use in controllers:

Summary