armature-framework 0.2.2

A modern, type-safe HTTP framework for Rust inspired by Angular and NestJS. Features dependency injection, decorators, middleware, authentication (JWT/OAuth2/SAML), validation, OpenAPI/Swagger, caching, job queues, and observability.
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# Error Transformation Guide

Armature provides a centralized error transformation system for consistent, configurable, and secure error handling across your application.

## Table of Contents

- [Overview]#overview
- [Features]#features
- [Basic Usage]#basic-usage
- [Response Formats]#response-formats
- [Error Response Structure]#error-response-structure
- [Error Transformer]#error-transformer
- [Validation Errors]#validation-errors
- [Sensitive Data Filtering]#sensitive-data-filtering
- [Custom Transformers]#custom-transformers
- [Preset Configurations]#preset-configurations
- [Best Practices]#best-practices
- [API Reference]#api-reference
- [Summary]#summary

## Overview

The error transformation system provides a centralized way to:

- Convert application errors to HTTP responses
- Format errors consistently (JSON, HTML, Problem Details)
- Filter sensitive information
- Add context and metadata
- Log errors appropriately

## Features

- ✅ Multiple response formats (JSON, plain text, HTML, RFC 7807)
- ✅ Configurable error response structure
- ✅ Sensitive data filtering (passwords, tokens, etc.)
- ✅ Validation error aggregation
- ✅ Custom error transformers
- ✅ Production/development modes
- ✅ Error logging integration
- ✅ Problem Details (RFC 7807) support

## Basic Usage

### Simple Error Transformation

```rust
use armature_core::{Error, HttpRequest, HttpResponse};
use armature_core::error_transform::{ErrorTransformer, ResponseFormat};

async fn handle_request(req: HttpRequest) -> Result<HttpResponse, Error> {
    // Your handler logic...
    Err(Error::NotFound("User not found".into()))
}

async fn error_handler(req: HttpRequest) -> HttpResponse {
    let transformer = ErrorTransformer::new()
        .format(ResponseFormat::Json)
        .production_mode(true);

    match handle_request(req.clone()).await {
        Ok(response) => response,
        Err(error) => transformer.transform(&error, &req),
    }
}
```

### Using ErrorResponseBuilder

For quick error responses:

```rust
use armature_core::error_transform::ErrorResponseBuilder;

// Quick error responses
let response = ErrorResponseBuilder::bad_request("Invalid input");
let response = ErrorResponseBuilder::not_found("Resource not found");
let response = ErrorResponseBuilder::internal_error("Something went wrong");
```

## Response Formats

Armature supports multiple standardized error response formats used across different platforms and specifications.

### JSON (Default)

Simple, clean JSON format for general use.

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::Json);
```

Response:
```json
{
  "status": 404,
  "message": "User not found",
  "error_type": "NOT_FOUND",
  "path": "/users/123",
  "timestamp": "2024-01-15T10:30:00Z",
  "request_id": "req-abc123"
}
```

### Plain Text

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::PlainText);
```

Response:
```
Error 404: User not found
Details: The requested resource could not be found
```

### HTML

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::Html);
```

Renders a styled HTML error page suitable for browser display.

### Problem Details (RFC 7807)

Standard IETF format for HTTP API problem details.

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::ProblemDetails);
```

Response (`application/problem+json`):
```json
{
  "type": "NOT_FOUND",
  "title": "User not found",
  "status": 404,
  "detail": "The requested user could not be found",
  "instance": "/users/123"
}
```

### JSON:API

JSON:API specification format (https://jsonapi.org/format/#errors).

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::JsonApi);
```

Response (`application/vnd.api+json`):
```json
{
  "errors": [
    {
      "status": "422",
      "code": "VALIDATION_ERROR",
      "title": "Validation failed",
      "detail": "Email format is invalid",
      "source": {
        "pointer": "/data/attributes/email"
      }
    }
  ]
}
```

### GraphQL

GraphQL specification error format (https://spec.graphql.org).

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::GraphQL);
```

Response:
```json
{
  "data": null,
  "errors": [
    {
      "message": "User not found",
      "path": ["/users/123"],
      "extensions": {
        "code": "NOT_FOUND",
        "status": 404
      }
    }
  ]
}
```

### Google/gRPC

Google Cloud API and gRPC-style error format.

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::Google);
```

Response:
```json
{
  "error": {
    "code": 400,
    "message": "Invalid argument",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest.FieldViolation",
        "field": "email",
        "description": "Invalid email format"
      }
    ]
  }
}
```

### AWS

AWS service-style error format.

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::Aws);
```

Response (`application/x-amz-json-1.1`):
```json
{
  "__type": "ValidationException",
  "message": "Validation error occurred",
  "Code": "VALIDATION_ERROR",
  "RequestId": "req-abc123"
}
```

### Azure

Microsoft Azure REST API error format.

```rust
let transformer = ErrorTransformer::new()
    .format(ResponseFormat::Azure);
```

Response:
```json
{
  "error": {
    "code": "InvalidInput",
    "message": "The request is invalid",
    "target": "/api/users",
    "details": [
      {
        "code": "ValidationError",
        "message": "Email is required",
        "target": "email"
      }
    ]
  }
}
```

### Format Comparison Table

| Format | Content-Type | Best For |
|--------|--------------|----------|
| `Json` | `application/json` | General APIs |
| `PlainText` | `text/plain` | CLI tools, debugging |
| `Html` | `text/html` | Browser-facing apps |
| `ProblemDetails` | `application/problem+json` | RESTful APIs (RFC 7807) |
| `JsonApi` | `application/vnd.api+json` | JSON:API implementations |
| `GraphQL` | `application/json` | GraphQL APIs |
| `Google` | `application/json` | Google Cloud/gRPC style |
| `Aws` | `application/x-amz-json-1.1` | AWS-compatible APIs |
| `Azure` | `application/json` | Azure-compatible APIs |

## Error Response Structure

### ErrorResponse

```rust
use armature_core::error_transform::{ErrorResponse, ValidationError};

let response = ErrorResponse::new(400)
    .message("Validation failed")
    .code("ERR_VALIDATION")
    .details("One or more fields are invalid")
    .error_type("VALIDATION_ERROR")
    .path("/api/users")
    .request_id("req-123")
    .with_metadata("field_count", 3)
    .with_validation_error(
        ValidationError::new("email", "Invalid email format")
    );
```

### Available Fields

| Field | Type | Description |
|-------|------|-------------|
| `status` | `u16` | HTTP status code |
| `code` | `Option<String>` | Application-specific error code |
| `message` | `String` | Human-readable message |
| `details` | `Option<String>` | Detailed description |
| `error_type` | `Option<String>` | Error category |
| `path` | `Option<String>` | Request path |
| `timestamp` | `Option<String>` | ISO 8601 timestamp |
| `request_id` | `Option<String>` | Request ID for tracing |
| `metadata` | `HashMap` | Additional data |
| `validation_errors` | `Vec` | Field validation errors |

## Error Transformer

### Configuration Options

```rust
let transformer = ErrorTransformer::new()
    // Response format
    .format(ResponseFormat::Json)

    // Production mode (hides internal details)
    .production_mode(true)

    // Include/exclude options
    .include_stack_trace(false)
    .include_path(true)
    .include_timestamp(true)

    // Security
    .filter_sensitive_data(true)

    // Custom error codes
    .map_error_code("NOT_FOUND", "ERR_404")
    .map_error_code("VALIDATION_ERROR", "ERR_422");
```

### Transform Methods

```rust
// Simple transform
let response = transformer.transform(&error, &request);

// Transform with context
let context = ErrorContext::from_request(request)
    .user_id("user-123")
    .with_data("operation", "create_user");

let response = transformer.transform_with_context(&error, &context);
```

## Validation Errors

### Single Validation Error

```rust
use armature_core::error_transform::ValidationError;

let error = ValidationError::new("email", "Invalid email format")
    .rule("email")
    .value("not-an-email");
```

### Multiple Validation Errors

```rust
use armature_core::error_transform::{ErrorResponse, ValidationError, ErrorResponseBuilder};

let response = ErrorResponseBuilder::validation_error(vec![
    ValidationError::new("email", "Invalid email format"),
    ValidationError::new("password", "Password must be at least 8 characters"),
    ValidationError::new("age", "Must be at least 18"),
]);
```

Response:
```json
{
  "status": 422,
  "message": "Validation failed",
  "error_type": "VALIDATION_ERROR",
  "validation_errors": [
    {
      "field": "email",
      "message": "Invalid email format"
    },
    {
      "field": "password",
      "message": "Password must be at least 8 characters"
    },
    {
      "field": "age",
      "message": "Must be at least 18"
    }
  ]
}
```

## Sensitive Data Filtering

The transformer automatically filters sensitive data when `filter_sensitive_data(true)`:

### Filtered Patterns

- Passwords: `password=secret``password=[FILTERED]`
- API keys: `api_key=abc123``api_key=[FILTERED]`
- Tokens: `token=xyz``token=[FILTERED]`
- Bearer tokens: `Bearer abc``Bearer [FILTERED]`
- Secrets: `secret=val``secret=[FILTERED]`
- Credit cards: `4111111111111111``[CARD FILTERED]`
- SSN: `123-45-6789``[SSN FILTERED]`

### Example

```rust
let transformer = ErrorTransformer::new()
    .filter_sensitive_data(true);

let error = Error::BadRequest(
    "Invalid request: password=secret123, api_key=abc".into()
);

// Error message becomes:
// "Invalid request: password=[FILTERED], api_key=[FILTERED]"
```

## Custom Transformers

### Add Custom Transform Logic

```rust
let transformer = ErrorTransformer::new()
    .with_transformer(|error, context| {
        // Custom logic for specific errors
        match error {
            Error::NotFound(_) => Some(
                ErrorResponse::new(404)
                    .message("Resource not found")
                    .code("RESOURCE_NOT_FOUND")
                    .with_metadata("suggestion", "Check the resource ID")
            ),
            Error::Unauthorized(_) => Some(
                ErrorResponse::new(401)
                    .message("Authentication required")
                    .with_metadata("auth_url", "/login")
            ),
            _ => None, // Fall through to default handling
        }
    });
```

### Add Custom Logging

```rust
let transformer = ErrorTransformer::new()
    .with_logger(|error, context, response| {
        // Custom logging logic
        if error.is_server_error() {
            eprintln!(
                "[ERROR] {} {} - {} ({})",
                context.request.method,
                context.request.path,
                error,
                response.request_id.as_deref().unwrap_or("unknown")
            );
        }
    });
```

### Add Error Filters

```rust
let transformer = ErrorTransformer::new()
    .with_filter(|error| {
        // Transform or filter errors
        match error {
            Error::Internal(msg) if msg.contains("database") => {
                Error::ServiceUnavailable("Service temporarily unavailable".into())
            }
            _ => error.clone(),
        }
    });
```

## Preset Configurations

### Development

```rust
let transformer = ErrorTransformer::development();
```

Features:
- Verbose error messages
- Stack traces enabled
- No sensitive data filtering
- Console logging

### Production

```rust
let transformer = ErrorTransformer::production();
```

Features:
- Generic messages for server errors
- No stack traces
- Sensitive data filtering
- Structured logging

### API

```rust
let transformer = ErrorTransformer::api();
```

Features:
- RFC 7807 Problem Details format
- Production mode
- Sensitive data filtering

## Best Practices

### 1. Use Production Mode in Production

```rust
let transformer = if cfg!(debug_assertions) {
    ErrorTransformer::development()
} else {
    ErrorTransformer::production()
};
```

### 2. Always Filter Sensitive Data

```rust
let transformer = ErrorTransformer::new()
    .filter_sensitive_data(true);  // Always enabled
```

### 3. Include Request IDs

```rust
// Add request ID middleware first
// Then error transformer will include it automatically
```

### 4. Map Error Codes for APIs

```rust
let transformer = ErrorTransformer::new()
    .map_error_code("NOT_FOUND", "RESOURCE_NOT_FOUND")
    .map_error_code("VALIDATION_ERROR", "INVALID_INPUT")
    .map_error_code("UNAUTHORIZED", "AUTH_REQUIRED");
```

### 5. Use Validation Errors for Form Inputs

```rust
// Instead of:
Err(Error::BadRequest("Email is invalid".into()))

// Use:
ErrorResponseBuilder::validation_error(vec![
    ValidationError::new("email", "Invalid email format")
        .rule("email")
        .value(email_input)
])
```

### 6. Log Server Errors, Not Client Errors

```rust
let transformer = ErrorTransformer::new()
    .with_logger(|error, ctx, response| {
        if error.is_server_error() {
            // Log full details for debugging
            tracing::error!(
                error = %error,
                path = ctx.request.path,
                "Server error"
            );
        }
        // Don't log 4xx errors (user mistakes)
    });
```

## Common Pitfalls

### ❌ Exposing Internal Details

```rust
// Bad: Exposes database schema
Err(Error::Internal("Column 'users.password_hash' not found".into()))

// Good: Generic message
Err(Error::Internal("Database error".into()))
```

### ❌ Including Sensitive Data in Errors

```rust
// Bad: Includes password in error
Err(Error::BadRequest(format!(
    "Invalid credentials for {}: password={}",
    email, password
)))

// Good: No sensitive data
Err(Error::Unauthorized("Invalid credentials".into()))
```

### ❌ Not Using Validation Errors

```rust
// Bad: Single error message for multiple issues
Err(Error::BadRequest("Email and password are invalid".into()))

// Good: Structured validation errors
ErrorResponseBuilder::validation_error(vec![
    ValidationError::new("email", "Invalid format"),
    ValidationError::new("password", "Too short"),
])
```

## API Reference

### Types

| Type | Description |
|------|-------------|
| `ErrorResponse` | Structured error response |
| `ErrorTransformer` | Central error transformer |
| `ErrorContext` | Request context for errors |
| `ValidationError` | Field validation error |
| `ResponseFormat` | Output format (JSON, HTML, etc.) |
| `ProblemDetails` | RFC 7807 structure |
| `ErrorResponseBuilder` | Quick error response builder |

### ErrorTransformer Methods

| Method | Description |
|--------|-------------|
| `new()` | Create with defaults |
| `development()` | Development preset |
| `production()` | Production preset |
| `api()` | API preset (RFC 7807) |
| `format(fmt)` | Set response format |
| `production_mode(bool)` | Enable/disable production mode |
| `filter_sensitive_data(bool)` | Enable/disable filtering |
| `with_transformer(fn)` | Add custom transformer |
| `with_logger(fn)` | Add custom logger |
| `transform(error, request)` | Transform error to response |

### ErrorResponse Methods

| Method | Description |
|--------|-------------|
| `new(status)` | Create new response |
| `message(msg)` | Set message |
| `code(code)` | Set error code |
| `details(details)` | Set details |
| `with_metadata(k, v)` | Add metadata |
| `with_validation_error(e)` | Add validation error |
| `to_json()` | Convert to JSON string |
| `to_html()` | Convert to HTML string |
| `into_http_response(fmt)` | Convert to HttpResponse |

## Summary

**Key Points:**

1. **Centralize error handling** with `ErrorTransformer`
2. **Use production mode** in production (hides internal details)
3. **Filter sensitive data** always
4. **Use validation errors** for form/input validation
5. **Choose appropriate format** (JSON for APIs, HTML for web)
6. **Log server errors** but not client errors
7. **Include request IDs** for debugging

**Quick Reference:**

```rust
use armature_core::error_transform::{
    ErrorTransformer, ErrorResponse, ValidationError,
    ErrorResponseBuilder, ResponseFormat
};

// Production transformer
let transformer = ErrorTransformer::production();

// Transform error
let response = transformer.transform(&error, &request);

// Quick errors
let response = ErrorResponseBuilder::not_found("User not found");

// Validation errors
let response = ErrorResponseBuilder::validation_error(vec![
    ValidationError::new("email", "Invalid format"),
]);

// Custom response
let response = ErrorResponse::new(400)
    .message("Bad request")
    .code("ERR_400")
    .into_http_response(ResponseFormat::Json);
```