neocrates 0.1.44

A comprehensive Rust library for various utilities and helpers
Documentation
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
448
449
450
use std::{fmt::Display, panic::Location};

use axum::{
    Json,
    http::StatusCode,
    response::{IntoResponse, Response},
};
use serde::Serialize;
use thiserror::Error;
use validator::ValidationErrors;

// 1. Common success status codes (2xx):
// - 200 for general success responses (GET/PUT/PATCH)
// - 201 for successful creation (POST)
// - 204 for no content responses (DELETE)
// - 202 for asynchronous task acceptance

// 2. Redirect status codes (3xx):
// - 301 for permanent resource relocation
// - 302 for temporary redirects
// - 304 for not modified, use cache
// - 307/308 for redirects preserving HTTP method

// 3. Client error status codes (4xx):
// - 400 for malformed requests (parameter errors)
// - 401 for unauthenticated requests (not logged in)
// - 403 for unauthorized access (logged in but insufficient permissions)
// - 404 for resource not found
// - 409 for resource conflicts (duplicate creation)

// 4. Server error status codes (5xx):
// - 500 for internal server errors (unexpected exceptions)
// - 502 for gateway errors (upstream service exceptions)
// - 503 for service unavailable (maintenance or overload)
// - 504 for gateway timeout (upstream service timeout)

// 5. Special purpose status codes:
// - 422 for business rule validation failures
// - 429 for request rate limits
// - 418 for fun easter egg responses

// 6. RESTful API common scenarios:
// - GET success returns 200
// - POST creation success returns 201
// - PUT/PATCH update success returns 200
// - DELETE success returns 204

// 7. Error handling principles:
// - Use 4xx series for client errors
// - Use 5xx series for server errors
// - Provide clear error messages and handling suggestions
// - Maintain uniform error response format

// 8. Status code selection criteria:
// - Prioritize semantically matching status codes
// - Maintain consistency within the application
// - Avoid uncommon status codes
// - Consider client compatibility

// 9. Cache-related status codes:
// - 304 used with ETag/Last-Modified
// - Cache control for GET requests
// - Use caching to improve performance

// 10. Security-related status codes:
// - 401 for authentication failures
// - 403 for permission verification failures
// - 429 for security rate limiting
// - 451 for legally prohibited access

pub type AppResult<T> = std::result::Result<T, AppError>;

// System error code enumeration
#[derive(Error, Debug)]
pub enum AppError {
    // Client errors (4xx)
    #[error("{0}")]
    ValidationError(String), // Parameter validation failure
    #[error("Unauthorized")]
    Unauthorized, // Not logged in or invalid token
    #[error("Token Expired")]
    TokenExpired,
    #[error("Forbidden")]
    Forbidden, // Insufficient permissions
    #[error("Resource not found: {0}")]
    NotFound(String), // Resource doesn't exist
    #[error("Request conflict: {0}")]
    Conflict(String), // Resource conflict
    #[error("{0}")]
    ClientError(String), // General client error
    #[error("{0}")]
    ClientDataError(String), // General client data error

    #[error("Business rule validation failed: {0}")]
    UnprocessableEntity(String), // 422: Business rule validation
    #[error("Rate limit exceeded: {0}")]
    RateLimit(String), // 429: Rate limit exceeded
    #[error("{0}")]
    EasterEgg(String), // 418: Fun easter egg responses

    // Server errors (5xx)
    #[error("Database error: {0}")]
    DbError(String), // Database error
    #[error("Redis error: {0}")]
    RedisError(String), // Redis error
    #[error("Message queue error: {0}")]
    MqError(String), // Message queue error
    #[error("External service error: {0}")]
    ExternalError(String), // External service call error
    #[error("Internal server error")]
    Internal(String), // Other internal errors

    #[error("{1}")]
    DataError(u32, String), // Custom business code and error message

    #[error("{0}")]
    JsonError(String), // JSON serialization error
}

// API response structure
#[derive(Serialize)]
pub struct ApiResponse<T> {
    pub code: u32,       // Business status code
    pub message: String, // Error message
    pub data: Option<T>, // Response data
}

// Error code and HTTP status code mapping
/// HTTP status code and business code mappings for application errors
impl AppError {
    // HTTP status code constants
    const HTTP_BAD_REQUEST: StatusCode = StatusCode::BAD_REQUEST; // 400
    const HTTP_UNAUTHORIZED: StatusCode = StatusCode::UNAUTHORIZED; // 401
    const HTTP_FORBIDDEN: StatusCode = StatusCode::FORBIDDEN; // 403
    const HTTP_NOT_FOUND: StatusCode = StatusCode::NOT_FOUND; // 404
    const HTTP_CONFLICT: StatusCode = StatusCode::CONFLICT; // 409
    const HTTP_UNPROCESSABLE_ENTITY: StatusCode = StatusCode::UNPROCESSABLE_ENTITY; // 422
    const HTTP_TOO_MANY_REQUESTS: StatusCode = StatusCode::TOO_MANY_REQUESTS; // 429
    const HTTP_IM_A_TEAPOT: StatusCode = StatusCode::IM_A_TEAPOT; // 418
    const EXPECTATION_FAILED: StatusCode = StatusCode::EXPECTATION_FAILED; // 417
    const HTTP_INTERNAL_ERROR: StatusCode = StatusCode::INTERNAL_SERVER_ERROR; // 500

    // Business error code constants
    const BIZ_VALIDATION_ERROR: u32 = 400001;
    const BIZ_UNAUTHORIZED: u32 = 400002;
    const BIZ_FORBIDDEN: u32 = 400003;
    const BIZ_NOT_FOUND: u32 = 400004;
    const BIZ_CONFLICT: u32 = 400005;
    const BIZ_CLIENT_ERROR: u32 = 400006;
    const BIZ_DATA_ERROR: u32 = 400007;
    const BIZ_TOKEN_EXPIRED: u32 = 400008;
    const BIZ_DB_ERROR: u32 = 500001;
    const BIZ_REDIS_ERROR: u32 = 500002;
    const BIZ_MQ_ERROR: u32 = 500003;
    const BIZ_EXTERNAL_ERROR: u32 = 500004;
    const BIZ_INTERNAL_ERROR: u32 = 500000;
    const BIZ_UNPROCESSABLE_ENTITY: u32 = 400100; // Business validation errors
    const BIZ_RATE_LIMIT: u32 = 400101; // Rate limiting errors
    const BIZ_EASTER_EGG: u32 = 400102; // Easter egg responses

    // Business data errors - Expanded categories
    // 410000-410099: Data existence errors
    pub const BIZ_DATA_EXISTS: u32 = 410000; // Data already exists
    pub const BIZ_DATA_DUPLICATE: u32 = 410001; // Duplicate entry found
    pub const BIZ_DATA_NOT_FOUND: u32 = 410002; // Data not found/doesn't exist
    pub const BIZ_DATA_DELETED: u32 = 410003; // Data has been deleted
    pub const BIZ_DATA_ARCHIVED: u32 = 410004; // Data is archived
    pub const BIZ_DATA_OUTDATED: u32 = 410005; // Data version is outdated

    // 410100-410199: JSON serialization errors
    pub const BIZ_JSON_ERROR: u32 = 410100; // JSON serialization error

    /// Maps application errors to HTTP status codes
    pub fn status_code(&self) -> StatusCode {
        match self {
            // 4xx Client Errors
            Self::ValidationError(_) => Self::HTTP_BAD_REQUEST,
            Self::Unauthorized => Self::HTTP_UNAUTHORIZED,
            Self::TokenExpired => Self::HTTP_UNAUTHORIZED,
            Self::Forbidden => Self::HTTP_FORBIDDEN,
            Self::NotFound(_) => Self::HTTP_NOT_FOUND,
            Self::Conflict(_) => Self::HTTP_CONFLICT,
            Self::UnprocessableEntity(_) => Self::HTTP_UNPROCESSABLE_ENTITY,
            Self::RateLimit(_) => Self::HTTP_TOO_MANY_REQUESTS,
            Self::EasterEgg(_) => Self::HTTP_IM_A_TEAPOT,
            Self::Internal(_) => Self::HTTP_INTERNAL_ERROR,
            Self::ClientError(_) => Self::EXPECTATION_FAILED,
            Self::DataError(_, _) => Self::HTTP_CONFLICT, // All data errors use HTTP 409
            // 4xx HTTP_BAD_REQUEST - Return 400 for all
            _ => Self::HTTP_BAD_REQUEST,
        }
    }

    /// Maps application errors to business error codes
    pub fn business_code(&self) -> u32 {
        match self {
            // 4xx Client Errors
            Self::ValidationError(_) => Self::BIZ_VALIDATION_ERROR,
            Self::Unauthorized => Self::BIZ_UNAUTHORIZED,
            Self::TokenExpired => Self::BIZ_TOKEN_EXPIRED,
            Self::Forbidden => Self::BIZ_FORBIDDEN,
            Self::NotFound(_) => Self::BIZ_NOT_FOUND,
            Self::Conflict(_) => Self::BIZ_CONFLICT,
            Self::UnprocessableEntity(_) => Self::BIZ_UNPROCESSABLE_ENTITY,
            Self::RateLimit(_) => Self::BIZ_RATE_LIMIT,
            Self::EasterEgg(_) => Self::BIZ_EASTER_EGG,
            Self::ClientError(_) => Self::BIZ_CLIENT_ERROR,
            Self::ClientDataError(_) => Self::BIZ_DATA_ERROR,
            Self::DataError(code, _) => *code, // Use the custom business code from DataError
            // 5xx Server Errors
            Self::DbError(_) => Self::BIZ_DB_ERROR,
            Self::RedisError(_) => Self::BIZ_REDIS_ERROR,
            Self::MqError(_) => Self::BIZ_MQ_ERROR,
            Self::ExternalError(_) => Self::BIZ_EXTERNAL_ERROR,
            Self::Internal(_) => Self::BIZ_INTERNAL_ERROR,
            // Business data errors
            // Self::DataExtis(_) => Self::BIZ_DATA_EXTIS,
            //
            Self::JsonError(_) => Self::BIZ_JSON_ERROR,
        }
    }

    /// Returns a user-friendly error message
    pub fn message(&self) -> String {
        match self {
            Self::UnprocessableEntity(msg) => msg.to_string(),
            Self::RateLimit(msg) => format!("Rate limit exceeded: {}", msg),
            Self::EasterEgg(msg) => format!("Easter egg: {}", msg),
            Self::ValidationError(msg) => msg.to_string(),
            Self::Unauthorized => "Unauthorized access".to_string(),
            Self::TokenExpired => "Token expired".to_string(),
            Self::Forbidden => "Access forbidden".to_string(),
            Self::NotFound(msg) => msg.to_string(),
            Self::Conflict(msg) => msg.to_string(),
            Self::DbError(e) => format!("Database error: {}", e),
            Self::RedisError(e) => format!("Cache error: {}", e),
            Self::MqError(e) => format!("Message queue error: {}", e),
            Self::ExternalError(e) => format!("External service error: {}", e),
            Self::Internal(e) => format!("Internal server error: {}", e),
            Self::ClientError(msg) => msg.to_string(),
            Self::ClientDataError(msg) => msg.to_string(),
            Self::DataError(_, msg) => msg.to_string(),
            Self::JsonError(msg) => format!("JSON serialization error: {}", msg),
        }
    }
}

// Implement response conversion
impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let status = self.status_code();
        let response = ApiResponse {
            code: self.business_code(),
            message: self.to_string(),
            data: None::<()>,
        };
        // Log the response
        tracing::error!(
            "...App Error...: code:{:?} message:{:?} self:{:?}",
            response.code,
            response.message,
            self
        );
        (status, Json(response)).into_response()
    }
}

impl From<ValidationErrors> for AppError {
    fn from(err: ValidationErrors) -> Self {
        tracing::warn!("Parameter validation failed: {:?}", err);
        let message = err
            .field_errors()
            .iter()
            .map(|(field, errors)| {
                let error_messages: Vec<String> = errors
                    .iter()
                    .filter_map(|error| error.message.as_ref().map(|m| m.to_string()))
                    .collect();
                format!("{}: {}", field, error_messages.join(", "))
            })
            .collect::<Vec<String>>()
            .join("; ");

        AppError::ValidationError(format!("Parameter validation failed: {}", message))
    }
}

#[cfg(any(feature = "diesel", feature = "full"))]
impl From<diesel::result::Error> for AppError {
    fn from(err: diesel::result::Error) -> Self {
        tracing::error!("Database error: {}", err);
        AppError::DbError(err.to_string())
    }
}

#[cfg(any(feature = "diesel", feature = "full"))]
impl From<deadpool_diesel::PoolError> for AppError {
    fn from(err: deadpool_diesel::PoolError) -> Self {
        tracing::error!("Deadpool_diesel Database error: {}", err);
        AppError::DbError(err.to_string())
    }
}

#[track_caller]
pub fn msg_with_location<M: Display>(msg: M) -> String {
    let loc = Location::caller();
    format!("{}:{} {}", loc.file(), loc.line(), msg)
}

impl AppError {
    #[track_caller]
    pub fn client_here<M: Display>(msg: M) -> Self {
        AppError::ClientError(msg_with_location(msg))
    }

    #[track_caller]
    pub fn data_here<M: Display>(msg: M) -> Self {
        AppError::ClientDataError(msg_with_location(msg))
    }

    #[track_caller]
    pub fn conflict_here<M: Display>(msg: M) -> Self {
        AppError::Conflict(msg_with_location(msg))
    }

    #[track_caller]
    pub fn not_found_here<M: Display>(msg: M) -> Self {
        AppError::NotFound(msg_with_location(msg))
    }
}

pub trait AppResultExt<T, E> {
    #[track_caller]
    fn client_context(self) -> AppResult<T>
    where
        E: Display;

    #[track_caller]
    fn context_msg(self, msg: impl Into<String>) -> AppResult<T>
    where
        E: Display;
}

impl<T, E> AppResultExt<T, E> for Result<T, E> {
    #[track_caller]
    fn client_context(self) -> AppResult<T>
    where
        E: Display,
    {
        self.map_err(|e| AppError::client_here(e))
    }

    #[track_caller]
    fn context_msg(self, msg: impl Into<String>) -> AppResult<T>
    where
        E: Display,
    {
        self.map_err(|e| AppError::client_here(format!("{} - {}", msg.into(), e)))
    }
}

// let chat_model: ChatModel = AgentChatService::get_agent_and_model(&app_state, pctx.aid)
//     .await
//     .map_err(AppError::client_here)?; // equivalent to |e| AppError::client_here(e)

// use crate::response::error::AppResultExt;

// let chat_model = AgentChatService::get_agent_and_model(&app_state, pctx.aid)
//     .await
//     .client_context()?; // automatically includes call site location

// impl Error for diesel::result::Error {
//     fn as_infra_error(&self) -> InfraError {
//         match self {
//             diesel::result::Error::NotFound => InfraError::NotFound,
//             _ => InfraError::InternalServerError,
//         }
//     }
// }

// impl Error for deadpool_diesel::PoolError {
//     fn as_infra_error(&self) -> InfraError {
//         InfraError::InternalServerError
//     }
// }

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn test_error_handling_examples() {
//         // 1. Business validation error (422)
//         let validation_error = AppError::UnprocessableEntity("Username already exists".to_string());
//         assert_eq!(
//             validation_error.status_code(),
//             StatusCode::UNPROCESSABLE_ENTITY
//         );
//         assert_eq!(validation_error.message(), "Username already exists");

//         // 2. Rate limiting error (429)
//         let rate_limit_error = AppError::RateLimit("Max 60 requests per minute".to_string());
//         assert_eq!(
//             rate_limit_error.status_code(),
//             StatusCode::TOO_MANY_REQUESTS
//         );
//         assert_eq!(
//             rate_limit_error.message(),
//             "Rate limit exceeded: Max 60 requests per minute"
//         );

//         // 3. Easter egg response (418)
//         let easter_egg = AppError::EasterEgg("I am a cute teapot".to_string());
//         assert_eq!(easter_egg.status_code(), StatusCode::IM_A_TEAPOT);
//         assert_eq!(easter_egg.message(), "Easter egg: I am a cute teapot");
//     }

//     // Usage example
//     async fn example_handler() -> Result<String, AppError> {
//         // 1. Business rule validation
//         if !is_valid_business_rule() {
//             return Err(AppError::UnprocessableEntity(
//                 "Input data does not meet business rules".to_string(),
//             ));
//         }

//         // 2. Rate limiting check
//         if is_rate_limited() {
//             return Err(AppError::RateLimit("Please try again later".to_string()));
//         }

//         // 3. Easter egg
//         if is_april_first() {
//             return Err(AppError::EasterEgg("It's April Fools' Day!".to_string()));
//         }

//         Ok("Processed successfully".to_string())
//     }

//     // Mock functions
//     fn is_valid_business_rule() -> bool {
//         true
//     }
//     fn is_rate_limited() -> bool {
//         false
//     }
//     fn is_april_first() -> bool {
//         false
//     }
// }