axum-helpers 0.1.0

Axum service building blocks: server lifecycle with graceful shutdown, health checks, structured errors, validated extractors, security middleware, rate limiting, and Prometheus metrics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Reusable OpenAPI response types for consistent API documentation.
//!
//! This module provides standardized response types that can be referenced in
//! `#[utoipa::path]` endpoint definitions. Each response type includes:
//! - HTTP status code mapping
//! - JSON schema definition
//! - Example response payload
//!
//! # Usage
//!
//! Reference these types in your OpenAPI path definitions:
//!
//! ```rust,ignore
//! #[utoipa::path(
//!     get,
//!     path = "/resource/{id}",
//!     responses(
//!         (status = 200, description = "Success", body = Resource),
//!         (status = 404, response = NotFoundResponse),
//!         (status = 500, response = InternalServerErrorResponse)
//!     )
//! )]
//! ```
//!
//! Also register them in your OpenAPI documentation:
//!
//! ```rust,ignore
//! #[derive(OpenApi)]
//! #[openapi(
//!     components(
//!         responses(
//!             NotFoundResponse,
//!             BadRequestValidationResponse,
//!             InternalServerErrorResponse
//!         )
//!     )
//! )]
//! ```

use super::ErrorResponse;
use utoipa::ToResponse;

// Note: json! macro is used in #[response] examples but compiler can't detect it
#[allow(unused_imports)]
use serde_json::json;

/// OpenAPI response type for 500 Internal Server Error.
///
/// Use this response when an unexpected server-side error occurs that prevents
/// the request from being fulfilled. Common scenarios include:
///
/// - Database connection failures
/// - Configuration errors
/// - Unexpected panics or exceptions
/// - Resource exhaustion
/// - Third-party service failures
///
/// The actual error details are logged server-side but not exposed to clients
/// for security reasons.
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     get,
///     path = "/resource",
///     responses(
///         (status = 200, description = "Success", body = Resource),
///         (status = 500, response = InternalServerErrorResponse)
///     )
/// )]
/// async fn get_resource() -> Result<Json<Resource>, AppError> {
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Internal Server Error",
    content_type = "application/json",
    example = json!({
        "code": 1005,
        "error": "INTERNAL_ERROR",
        "message": "An internal server error occurred",
        "details": null
    })
)]
pub struct InternalServerErrorResponse(pub ErrorResponse);

/// OpenAPI response type for 400 Bad Request - Validation Error.
///
/// Use this response when request body validation fails. This typically occurs when
/// using the `ValidatedJson` extractor with the `validator` crate.
///
/// The `details` field contains structured validation errors with:
/// - Field name as the key
/// - Array of validation failures for that field
/// - Each failure includes: validation code, message, and relevant parameters
///
/// # Common Validation Scenarios
///
/// - String length constraints (min, max)
/// - Email format validation
/// - Required fields missing
/// - Numeric range violations
/// - Custom validation rules
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     post,
///     path = "/resource",
///     request_body = CreateResourceRequest,
///     responses(
///         (status = 201, description = "Created", body = Resource),
///         (status = 400, response = BadRequestValidationResponse)
///     )
/// )]
/// async fn create_resource(
///     ValidatedJson(input): ValidatedJson<CreateResourceRequest>
/// ) -> Result<Json<Resource>, AppError> {
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Bad Request - Validation Error",
    content_type = "application/json",
    example = json!({
        "code": 1001,
        "error": "VALIDATION_ERROR",
        "message": "Request validation failed",
        "details": {
            "title": [{
                "code": "length",
                "message": "length is less than 3",
                "params": {"min": 3, "value": "ab"}
            }]
        }
    })
)]
pub struct BadRequestValidationResponse(pub ErrorResponse);

/// OpenAPI response type for 400 Bad Request - Invalid UUID.
///
/// Use this response when a path or query parameter expected to be a UUID
/// cannot be parsed. This typically occurs when using the `UuidPath` extractor.
///
/// # Common Scenarios
///
/// - Malformed UUID string (wrong length, invalid characters)
/// - Using non-UUID values in UUID path parameters
/// - Invalid UUID format in query parameters
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     get,
///     path = "/resource/{id}",
///     params(
///         ("id" = Uuid, Path, description = "Resource ID")
///     ),
///     responses(
///         (status = 200, description = "Found", body = Resource),
///         (status = 400, response = BadRequestUuidResponse),
///         (status = 404, response = NotFoundResponse)
///     )
/// )]
/// async fn get_resource(
///     UuidPath(id): UuidPath
/// ) -> Result<Json<Resource>, AppError> {
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Bad Request - Invalid UUID",
    content_type = "application/json",
    example = json!({
        "code": 1002,
        "error": "INVALID_UUID",
        "message": "Invalid UUID format",
        "details": null
    })
)]
pub struct BadRequestUuidResponse(pub ErrorResponse);

/// OpenAPI response type for 404 Not Found.
///
/// Use this response when the requested resource does not exist in the system.
/// This is typically returned when:
///
/// - A database query returns no results for a given ID
/// - A user attempts to access a resource that was deleted
/// - An endpoint path exists but the specific resource identifier is invalid
///
/// # Best Practices
///
/// - Return 404 for missing resources, not 400
/// - Don't expose whether a resource exists for security reasons (unless appropriate)
/// - Use consistent error messages
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     get,
///     path = "/resource/{id}",
///     responses(
///         (status = 200, description = "Found", body = Resource),
///         (status = 404, response = NotFoundResponse)
///     )
/// )]
/// async fn get_resource(id: Uuid) -> Result<Json<Resource>, AppError> {
///     let resource = service.find(id).await
///         .ok_or_else(|| AppError::NotFound(format!("Resource {} not found", id)))?;
///     Ok(Json(resource))
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Resource not found",
    content_type = "application/json",
    example = json!({
        "code": 1004,
        "error": "NOT_FOUND",
        "message": "Resource not found",
        "details": null
    })
)]
pub struct NotFoundResponse(pub ErrorResponse);

/// OpenAPI response type for 401 Unauthorized.
///
/// Use this response when authentication is required but not provided, or when
/// the provided authentication credentials are invalid.
///
/// # Common Scenarios
///
/// - Missing authentication token (JWT, session cookie, API key)
/// - Expired authentication token
/// - Invalid credentials (wrong username/password)
/// - Malformed authentication header
///
/// # HTTP 401 vs 403
///
/// - **401 Unauthorized**: Authentication is missing or invalid. The client should
///   authenticate and retry.
/// - **403 Forbidden**: Authentication succeeded but the user lacks permission.
///   Retrying with the same credentials will not help.
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     get,
///     path = "/protected/resource",
///     responses(
///         (status = 200, description = "Success", body = Resource),
///         (status = 401, response = UnauthorizedResponse)
///     ),
///     security(("bearer_auth" = []))
/// )]
/// async fn protected_resource(
///     auth: AuthenticatedUser
/// ) -> Result<Json<Resource>, AppError> {
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Unauthorized - Authentication required",
    content_type = "application/json",
    example = json!({
        "code": 1006,
        "error": "UNAUTHORIZED",
        "message": "Authentication required",
        "details": null
    })
)]
pub struct UnauthorizedResponse(pub ErrorResponse);

/// OpenAPI response type for 403 Forbidden.
///
/// Use this response when the authenticated user does not have sufficient permissions
/// to perform the requested action. The user is authenticated (unlike 401) but lacks
/// the necessary authorization.
///
/// # Common Scenarios
///
/// - User attempting to access another user's resources
/// - Role-based access control (RBAC) violations
/// - Tenant isolation enforcement
/// - Rate limiting or quota exceeded
/// - Disabled or suspended account
///
/// # HTTP 401 vs 403
///
/// - **401 Unauthorized**: No valid authentication provided
/// - **403 Forbidden**: Valid authentication but insufficient permissions
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     delete,
///     path = "/resource/{id}",
///     responses(
///         (status = 204, description = "Deleted"),
///         (status = 403, response = ForbiddenResponse),
///         (status = 404, response = NotFoundResponse)
///     )
/// )]
/// async fn delete_resource(
///     auth: AuthenticatedUser,
///     id: Uuid
/// ) -> Result<StatusCode, AppError> {
///     if !auth.can_delete(id) {
///         return Err(AppError::Forbidden("Insufficient permissions".to_string()));
///     }
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Forbidden - Insufficient permissions",
    content_type = "application/json",
    example = json!({
        "code": 1007,
        "error": "FORBIDDEN",
        "message": "Access forbidden",
        "details": null
    })
)]
pub struct ForbiddenResponse(pub ErrorResponse);

/// OpenAPI response type for 409 Conflict.
///
/// Use this response when the request cannot be completed due to a conflict with
/// the current state of the resource. This is commonly used for:
///
/// # Common Scenarios
///
/// - **Duplicate Resources**: Attempting to create a resource that already exists
///   (e.g., username, email, unique name)
/// - **Concurrent Updates**: Version conflicts in optimistic locking scenarios
/// - **State Conflicts**: Invalid state transitions (e.g., deleting an active resource
///   that must be suspended first)
/// - **Unique Constraint Violations**: Database unique constraint violations
///
/// # Best Practices
///
/// - Provide helpful error messages indicating what caused the conflict
/// - Include the conflicting field name when possible
/// - Suggest corrective actions to the client
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     post,
///     path = "/projects",
///     request_body = CreateProject,
///     responses(
///         (status = 201, description = "Created", body = Project),
///         (status = 409, response = ConflictResponse)
///     )
/// )]
/// async fn create_project(
///     input: ValidatedJson<CreateProject>
/// ) -> Result<Json<Project>, AppError> {
///     // Check for duplicate name
///     if service.exists_by_name(&input.name).await? {
///         return Err(AppError::Conflict(
///             format!("Project with name '{}' already exists", input.name)
///         ));
///     }
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Conflict - Resource already exists",
    content_type = "application/json",
    example = json!({
        "code": 1008,
        "error": "CONFLICT",
        "message": "Resource already exists",
        "details": null
    })
)]
pub struct ConflictResponse(pub ErrorResponse);

/// OpenAPI response type for 503 Service Unavailable.
///
/// Use this response when the service is temporarily unable to handle requests.
/// This indicates a temporary condition that should resolve itself.
///
/// # Common Scenarios
///
/// - Database connection pool exhausted
/// - Dependency service is down (circuit breaker open)
/// - Server is overloaded or throttling requests
/// - Maintenance mode or graceful shutdown in progress
/// - Health check failures
///
/// # Client Behavior
///
/// Clients receiving 503 should:
/// - Retry the request after a delay
/// - Check the `Retry-After` header if provided
/// - Use exponential backoff for retries
/// - Consider circuit breaker patterns
///
/// # Example Usage
///
/// ```rust,ignore
/// #[utoipa::path(
///     get,
///     path = "/resource",
///     responses(
///         (status = 200, description = "Success", body = Vec<Resource>),
///         (status = 503, response = ServiceUnavailableResponse)
///     )
/// )]
/// async fn list_resources(
///     State(db): State<DatabasePool>
/// ) -> Result<Json<Vec<Resource>>, AppError> {
///     // Connection pool timeout returns ServiceUnavailable
///     let conn = db.acquire().await
///         .map_err(|e| AppError::ServiceUnavailable(e.to_string()))?;
///     // ... implementation
/// }
/// ```
#[derive(ToResponse)]
#[response(
    description = "Service Unavailable",
    content_type = "application/json",
    example = json!({
        "code": 1011,
        "error": "SERVICE_UNAVAILABLE",
        "message": "Service is temporarily unavailable",
        "details": null
    })
)]
pub struct ServiceUnavailableResponse(pub ErrorResponse);