Skip to main content

forgedb_validation/
status.rs

1//! HTTP status code mapping for validation errors
2
3/// Map common validation errors to HTTP status codes
4pub struct StatusCodeMapper;
5
6impl StatusCodeMapper {
7    /// Get status code for validation error type
8    pub fn for_validation_error(error_type: &str) -> u16 {
9        match error_type {
10            "required_field" => 400,        // Bad Request
11            "invalid_format" => 400,        // Bad Request
12            "invalid_type" => 400,          // Bad Request
13            "out_of_range" => 400,          // Bad Request
14            "not_found" => 404,             // Not Found
15            "already_exists" => 409,        // Conflict
16            "unique_violation" => 409,      // Conflict
17            "foreign_key_violation" => 422, // Unprocessable Entity
18            "internal_error" => 500,        // Internal Server Error
19            _ => 400,                       // Default to Bad Request
20        }
21    }
22
23    /// Get status code name
24    pub fn status_name(code: u16) -> &'static str {
25        match code {
26            200 => "OK",
27            201 => "Created",
28            204 => "No Content",
29            400 => "Bad Request",
30            401 => "Unauthorized",
31            403 => "Forbidden",
32            404 => "Not Found",
33            409 => "Conflict",
34            422 => "Unprocessable Entity",
35            500 => "Internal Server Error",
36            502 => "Bad Gateway",
37            503 => "Service Unavailable",
38            _ => "Unknown",
39        }
40    }
41
42    /// Check if status code indicates success
43    pub fn is_success(code: u16) -> bool {
44        code >= 200 && code < 300
45    }
46
47    /// Check if status code indicates client error
48    pub fn is_client_error(code: u16) -> bool {
49        code >= 400 && code < 500
50    }
51
52    /// Check if status code indicates server error
53    pub fn is_server_error(code: u16) -> bool {
54        code >= 500 && code < 600
55    }
56}
57