Response type enables using Rust’s ? operator for clean error propagation.
The Response Type
In Kit, controller handlers return aResponse type, which is an alias for Result<HttpResponse, HttpResponse>:
- Return successful responses with
Ok(HttpResponse::...) - Return error responses with
Err(HttpResponse::...) - Use the
?operator for automatic error conversion
Creating Responses
JSON Responses
The most common response type. UseHttpResponse::json() or the json_response! macro:
json_response! macro:
Text Responses
For plain text responses:text_response! macro:
Setting Status Codes
Chain.status() to set the HTTP status code:
Adding Headers
Add custom headers with.header():
Redirects
Kit provides two ways to create redirects:Simple Redirects
Redirect to a specific URL or path:Named Route Redirects
Redirect to a named route using theredirect! macro (with compile-time route validation):
Redirects with Parameters
Add route parameters and query strings:Permanent Redirects
Use.permanent() for 301 redirects (default is 302):
Error Handling
Kit automatically converts errors to appropriate HTTP responses when using the? operator.
Using the ? Operator
Errors are automatically converted to JSON error responses:
AppError for Custom Errors
UseAppError for domain-specific errors with custom status codes:
AppError Helper Methods
| Method | Status Code | Use Case |
|---|---|---|
AppError::new(msg) | 500 | Generic server error |
AppError::not_found(msg) | 404 | Resource not found |
AppError::bad_request(msg) | 400 | Invalid request |
AppError::unauthorized(msg) | 401 | Authentication required |
AppError::forbidden(msg) | 403 | Access denied |
AppError::unprocessable(msg) | 422 | Validation failed |
AppError::conflict(msg) | 409 | Resource conflict |
Custom Status Codes
Set any status code with.status():
FrameworkError Types
Kit’sFrameworkError handles common error scenarios:
Error Response Format
Errors are returned as JSON with appropriate structure:Implementing HttpError Trait
For custom error types, implement theHttpError trait:
Response Macros
Kit provides convenient macros for creating responses:| Macro | Description | Example |
|---|---|---|
json_response! | Create a JSON response | json_response!({"key": "value"}) |
text_response! | Create a text response | text_response!("Hello") |
redirect! | Redirect to named route | redirect!("users.index").into() |
Complete Example
Summary
| Feature | Usage |
|---|---|
| JSON response | HttpResponse::json(value) or json_response!({...}) |
| Text response | HttpResponse::text(str) or text_response!(str) |
| Set status | .status(code) |
| Add header | .header(name, value) |
| Simple redirect | Redirect::to(path).into() |
| Named redirect | redirect!("route.name").into() |
| With route params | .with("key", "value") |
| With query params | .query("key", "value") |
| Permanent redirect | .permanent() |
| Not found error | AppError::not_found(msg) |
| Bad request | AppError::bad_request(msg) |
| Unauthorized | AppError::unauthorized(msg) |
| Custom status | AppError::new(msg).status(code) |