waddling-errors 0.7.3

Structured, secure-by-default diagnostic codes for distributed systems with no_std and role-based documentation
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
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# Catalog Guide - Network Efficiency & Internationalization

## Overview

The Catalog Renderer generates compact lookup tables that map error hashes to their documentation. This enables efficient error transmission over networks, multi-language support, and offline-first applications.

**Key Benefits:**
- ๐Ÿ“‰ **80% smaller payloads** - Send hash + fields instead of full error messages
- ๐ŸŒ **Multi-language support** - Same hash, different language catalogs
- ๐Ÿ“ฑ **Offline-first** - Cache catalog locally for offline error display
- ๐Ÿ”‹ **Battery/bandwidth savings** - Critical for mobile and IoT devices
- ๐Ÿš€ **Faster transmission** - 40 bytes vs 200+ bytes per error

---

## Use Cases

### 1. IoT Devices

**Problem**: IoT devices have limited bandwidth and battery.

**Solution**: Send compact error codes, expand on server.

```text
Device โ†’ Server
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
Payload: "jGKFp,45.2"  (12 bytes)

Server expands with catalog:
"E.SENSOR.TEMP.OVERHEAT: Temperature 45.2ยฐC exceeds threshold"
```

**Savings**: 12 bytes vs 65+ bytes (81% reduction)

### 2. Mobile Applications

**Problem**: Mobile apps need to minimize data usage and support offline mode.

**Solution**: Download catalog once, receive compact error hashes from API.

```text
App Launch:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
1. Download catalog once (50 KB gzipped)
2. Cache locally with version check

API Requests:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
Server response: {"h": "jGKFp", "f": {"temp": "45.2"}}  (40 bytes)
App expands using cached catalog

Offline Mode:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
App uses cached catalog to display errors
```

**Savings**: 40 bytes per error vs 200+ bytes (80% reduction)

### 3. Multi-Language Support (i18n)

**Problem**: Same error needs different messages per language.

**Solution**: Generate separate catalogs per language, client picks appropriate one.

```text
Server generates:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
- catalog-en.json  (English)
- catalog-es.json  (Spanish)
- catalog-fr.json  (French)
- catalog-de.json  (German)

Client receives: {"h": "jGKFp", "f": {"temp": "45.2"}}

English catalog: "Temperature 45.2ยฐC exceeds threshold"
Spanish catalog: "Temperatura 45.2ยฐC supera el umbral"
French catalog:  "Tempรฉrature 45.2ยฐC dรฉpasse le seuil"
German catalog:  "Temperatur 45.2ยฐC รผberschreitet Schwellenwert"
```

**Same hash works with all languages!**

### 4. Microservices Architecture

**Problem**: Multiple services need consistent error codes.

**Solution**: Central error catalog, services send hashes, API gateway expands.

```text
Service A โ†’ Gateway โ†’ Client
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
Service: {"h": "jGKFp", "f": {...}}
Gateway: Expands with catalog โ†’ Full error message
Client: Receives human-readable error
```

### 5. Offline-First Progressive Web Apps

**Problem**: PWA needs to display errors when offline.

**Solution**: Service worker caches catalog, expands errors locally.

```text
Online:
โ”โ”โ”โ”โ”โ”โ”
1. Service worker downloads latest catalog
2. Stores in IndexedDB with version

Offline:
โ”โ”โ”โ”โ”โ”โ”โ”
1. API call fails โ†’ fallback to cached response
2. Response contains error hash
3. Service worker expands using cached catalog
4. User sees proper error message
```

---

## Catalog Formats

### Full Format

Complete documentation with all fields. Best for development/debugging.

```rust
use waddling_errors::doc_generator::{CatalogRenderer, CatalogFormat};

let catalog = CatalogRenderer::new(CatalogFormat::Full);
registry.render(vec![Box::new(catalog)], "target/catalog")?;
```

**Output** (`catalog-pub.json`):

```json
{
  "version": "1.0.0",
  "generated": "2024-11-19T15:00:00Z",
  "algorithm": "base62-ahash-v1",
  "role": "Public",
  "errors": {
    "jGKFp": {
      "code": "E.AUTH.TOKEN.EXPIRED",
      "severity": "Error",
      "message": "JWT token expired at {expiry}",
      "description": "Your session has expired. Please log in again.",
      "hints": ["Use refresh token endpoint", "Check token expiration time"],
      "tags": ["auth", "security", "session"],
      "docs_url": "https://docs.example.com/errors/E.AUTH.TOKEN.EXPIRED"
    }
  }
}
```

**Size**: ~200 bytes per error

### Compact Format

Minimal fields with abbreviated keys. Best for production.

```rust
let catalog = CatalogRenderer::new(CatalogFormat::Compact);
registry.render(vec![Box::new(catalog)], "target/catalog")?;
```

**Output**:

```json
{
  "v": "1.0.0",
  "a": "base62-ahash-v1",
  "r": "Public",
  "e": {
    "jGKFp": {
      "c": "E.AUTH.TOKEN.EXPIRED",
      "s": "E",
      "m": "JWT token expired at {expiry}",
      "d": "Your session has expired. Please log in again.",
      "h": ["Use refresh token endpoint"]
    }
  }
}
```

**Size**: ~120 bytes per error (40% smaller than Full)

### Minimal Format

Only essential fields. Best for IoT/embedded systems.

```rust
let catalog = CatalogRenderer::new(CatalogFormat::Minimal);
registry.render(vec![Box::new(catalog)], "target/catalog")?;
```

**Output**:

```json
{
  "jGKFp": ["E.AUTH.TOKEN.EXPIRED", "JWT token expired at {expiry}"]
}
```

**Size**: ~60 bytes per error (70% smaller than Full)

---

## Server-Side Implementation

### Generating Catalogs

```rust
use waddling_errors::doc_generator::{DocRegistry, CatalogRenderer, CatalogFormat};

fn generate_catalogs() -> Result<(), Box<dyn std::error::Error>> {
    let mut registry = DocRegistry::new("MyAPI", "1.0.0");
    
    // Register all your errors
    register_all_errors(&mut registry)?;
    
    // Generate catalogs for all roles
    let catalog = CatalogRenderer::new(CatalogFormat::Compact);
    registry.render_all_roles(vec![Box::new(catalog)], "target/catalog")?;
    
    // Generates:
    //   catalog-pub.json  - Public errors
    //   catalog-dev.json  - Developer errors
    //   catalog-int.json  - Internal errors
    
    Ok(())
}
```

### Serving Catalogs

**Option 1: Static Files**

```rust
// Axum example
use axum::{Router, routing::get};
use tower_http::services::ServeDir;

let app = Router::new()
    .nest_service("/catalogs", ServeDir::new("target/catalog"));
```

**Option 2: Embedded in Binary**

```rust
// Embed at compile time
const CATALOG: &str = include_str!("../target/catalog/catalog-pub.json");

async fn get_catalog() -> impl IntoResponse {
    (
        StatusCode::OK,
        [(header::CONTENT_TYPE, "application/json")],
        CATALOG
    )
}
```

**Option 3: CDN Distribution**

```bash
# Upload to CDN
aws s3 cp target/catalog/catalog-pub.json s3://cdn.example.com/v1.0.0/

# Client downloads from CDN
curl https://cdn.example.com/v1.0.0/catalog-pub.json
```

### API Response Format

**Server sends compact error:**

```rust
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct ApiError {
    h: String,              // Hash
    f: HashMap<String, String>,  // Fields
    #[serde(skip_serializing_if = "Option::is_none")]
    ts: Option<u64>,        // Timestamp
}

// Usage
let error = ApiError {
    h: "jGKFp".to_string(),
    f: [("expiry", "2024-11-19T15:00:00Z")].into_iter()
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect(),
    ts: Some(1700406000),
};

// JSON: {"h":"jGKFp","f":{"expiry":"2024-11-19T15:00:00Z"},"ts":1700406000}
// Size: ~72 bytes
```

---

## Client-Side Implementation

### JavaScript/TypeScript

```typescript
// catalog-client.ts
interface CatalogEntry {
  c: string;   // code
  s: string;   // severity
  m: string;   // message
  d?: string;  // description
  h?: string[];  // hints
}

interface Catalog {
  v: string;
  a: string;
  r: string;
  e: Record<string, CatalogEntry>;
}

class ErrorCatalog {
  private catalog: Catalog | null = null;
  private version: string;

  constructor(version: string) {
    this.version = version;
  }

  async load(): Promise<void> {
    // Try cache first
    const cached = localStorage.getItem(`catalog-${this.version}`);
    if (cached) {
      this.catalog = JSON.parse(cached);
      return;
    }

    // Download from server
    const response = await fetch(`/catalogs/catalog-pub.json`);
    this.catalog = await response.json();
    
    // Cache for offline use
    localStorage.setItem(`catalog-${this.version}`, JSON.stringify(this.catalog));
  }

  expand(hash: string, fields: Record<string, string>): string {
    if (!this.catalog) {
      throw new Error("Catalog not loaded");
    }

    const entry = this.catalog.e[hash];
    if (!entry) {
      return `Unknown error: ${hash}`;
    }

    // Replace field placeholders
    let message = entry.m;
    for (const [key, value] of Object.entries(fields)) {
      message = message.replace(`{${key}}`, value);
    }

    return message;
  }

  getDetails(hash: string) {
    if (!this.catalog) {
      throw new Error("Catalog not loaded");
    }

    return this.catalog.e[hash];
  }
}

// Usage
const catalog = new ErrorCatalog("1.0.0");
await catalog.load();

// API returns: {"h":"jGKFp","f":{"expiry":"2024-11-19T15:00:00Z"}}
const apiError = await response.json();
const message = catalog.expand(apiError.h, apiError.f);
console.log(message); // "JWT token expired at 2024-11-19T15:00:00Z"
```

### Swift (iOS)

```swift
// ErrorCatalog.swift
struct CatalogEntry: Codable {
    let c: String   // code
    let s: String   // severity
    let m: String   // message
    let d: String?  // description
    let h: [String]?  // hints
}

struct Catalog: Codable {
    let v: String
    let a: String
    let r: String
    let e: [String: CatalogEntry]
}

class ErrorCatalog {
    private var catalog: Catalog?
    
    func load() async throws {
        // Try cache first
        if let cached = UserDefaults.standard.data(forKey: "error_catalog") {
            catalog = try JSONDecoder().decode(Catalog.self, from: cached)
            return
        }
        
        // Download from server
        let url = URL(string: "https://api.example.com/catalogs/catalog-pub.json")!
        let (data, _) = try await URLSession.shared.data(from: url)
        catalog = try JSONDecoder().decode(Catalog.self, from: data)
        
        // Cache for offline use
        UserDefaults.standard.set(data, forKey: "error_catalog")
    }
    
    func expand(hash: String, fields: [String: String]) -> String {
        guard let catalog = catalog,
              let entry = catalog.e[hash] else {
            return "Unknown error: \(hash)"
        }
        
        var message = entry.m
        for (key, value) in fields {
            message = message.replacingOccurrences(of: "{\(key)}", with: value)
        }
        
        return message
    }
}

// Usage
let catalog = ErrorCatalog()
try await catalog.load()

// API returns: {"h":"jGKFp","f":{"expiry":"2024-11-19T15:00:00Z"}}
let message = catalog.expand(hash: apiError.h, fields: apiError.f)
print(message) // "JWT token expired at 2024-11-19T15:00:00Z"
```

### Kotlin (Android)

```kotlin
// ErrorCatalog.kt
data class CatalogEntry(
    val c: String,   // code
    val s: String,   // severity
    val m: String,   // message
    val d: String?,  // description
    val h: List<String>?  // hints
)

data class Catalog(
    val v: String,
    val a: String,
    val r: String,
    val e: Map<String, CatalogEntry>
)

class ErrorCatalog(private val context: Context) {
    private var catalog: Catalog? = null
    
    suspend fun load() {
        // Try cache first
        val prefs = context.getSharedPreferences("error_catalog", Context.MODE_PRIVATE)
        val cached = prefs.getString("catalog", null)
        if (cached != null) {
            catalog = Gson().fromJson(cached, Catalog::class.java)
            return
        }
        
        // Download from server
        val response = ktorClient.get("https://api.example.com/catalogs/catalog-pub.json")
        catalog = response.body()
        
        // Cache for offline use
        prefs.edit().putString("catalog", Gson().toJson(catalog)).apply()
    }
    
    fun expand(hash: String, fields: Map<String, String>): String {
        val entry = catalog?.e?.get(hash) ?: return "Unknown error: $hash"
        
        var message = entry.m
        fields.forEach { (key, value) ->
            message = message.replace("{$key}", value)
        }
        
        return message
    }
}

// Usage
val catalog = ErrorCatalog(context)
catalog.load()

// API returns: {"h":"jGKFp","f":{"expiry":"2024-11-19T15:00:00Z"}}
val message = catalog.expand(apiError.h, apiError.f)
println(message) // "JWT token expired at 2024-11-19T15:00:00Z"
```

---

## Multi-Language Support

### Generating Language-Specific Catalogs

```rust
use waddling_errors::doc_generator::{DocRegistry, CatalogRenderer, CatalogFormat};

fn generate_i18n_catalogs() -> Result<(), Box<dyn std::error::Error>> {
    // English catalog
    let mut registry_en = DocRegistry::new("MyAPI", "1.0.0");
    register_errors_en(&mut registry_en)?;
    
    let catalog = CatalogRenderer::new(CatalogFormat::Compact);
    registry_en.render(vec![Box::new(catalog)], "target/catalog/en")?;
    
    // Spanish catalog
    let mut registry_es = DocRegistry::new("MyAPI", "1.0.0");
    register_errors_es(&mut registry_es)?;
    
    let catalog = CatalogRenderer::new(CatalogFormat::Compact);
    registry_es.render(vec![Box::new(catalog)], "target/catalog/es")?;
    
    // French catalog
    let mut registry_fr = DocRegistry::new("MyAPI", "1.0.0");
    register_errors_fr(&mut registry_fr)?;
    
    let catalog = CatalogRenderer::new(CatalogFormat::Compact);
    registry_fr.render(vec![Box::new(catalog)], "target/catalog/fr")?;
    
    Ok(())
}

fn register_errors_en(registry: &mut DocRegistry) -> Result<(), Box<dyn std::error::Error>> {
    registry.register_code_extended(
        &ERR_TOKEN_EXPIRED,
        "JWT token expired at {expiry}",
        &["Use refresh token endpoint"],
        &["auth"],
        Some(Role::Public),
        &[], None, &[],
    )?;
    Ok(())
}

fn register_errors_es(registry: &mut DocRegistry) -> Result<(), Box<dyn std::error::Error>> {
    registry.register_code_extended(
        &ERR_TOKEN_EXPIRED,
        "Token JWT expirado en {expiry}",
        &["Usar endpoint de actualizaciรณn de token"],
        &["auth"],
        Some(Role::Public),
        &[], None, &[],
    )?;
    Ok(())
}

fn register_errors_fr(registry: &mut DocRegistry) -> Result<(), Box<dyn std::error::Error>> {
    registry.register_code_extended(
        &ERR_TOKEN_EXPIRED,
        "Token JWT expirรฉ ร  {expiry}",
        &["Utiliser le point de terminaison de rafraรฎchissement du token"],
        &["auth"],
        Some(Role::Public),
        &[], None, &[],
    )?;
    Ok(())
}
```

### Client Language Selection

```typescript
class MultiLangCatalog {
  private catalogs: Map<string, Catalog> = new Map();
  private currentLang: string = 'en';

  async loadLanguage(lang: string): Promise<void> {
    if (this.catalogs.has(lang)) {
      this.currentLang = lang;
      return;
    }

    const response = await fetch(`/catalogs/${lang}/catalog-pub.json`);
    const catalog = await response.json();
    this.catalogs.set(lang, catalog);
    this.currentLang = lang;
  }

  expand(hash: string, fields: Record<string, string>): string {
    const catalog = this.catalogs.get(this.currentLang);
    if (!catalog) {
      throw new Error(`Catalog not loaded for language: ${this.currentLang}`);
    }

    const entry = catalog.e[hash];
    if (!entry) {
      return `Unknown error: ${hash}`;
    }

    let message = entry.m;
    for (const [key, value] of Object.entries(fields)) {
      message = message.replace(`{${key}}`, value);
    }

    return message;
  }

  setLanguage(lang: string): void {
    if (!this.catalogs.has(lang)) {
      throw new Error(`Language not loaded: ${lang}`);
    }
    this.currentLang = lang;
  }
}

// Usage
const catalog = new MultiLangCatalog();
await catalog.loadLanguage('en');
await catalog.loadLanguage('es');

// Switch to Spanish
catalog.setLanguage('es');
const message = catalog.expand(apiError.h, apiError.f);
// "Token JWT expirado en 2024-11-19T15:00:00Z"
```

---

## Performance Optimizations

### Compression

Catalogs compress very well with gzip:

```bash
# Original: 50 KB
gzip catalog-pub.json

# Compressed: ~8 KB (84% reduction)
```

### Versioning & Caching

```rust
// Include version in filename
let catalog = CatalogRenderer::new(CatalogFormat::Compact)
    .with_version("1.0.0");
registry.render(vec![Box::new(catalog)], "target/catalog")?;

// Generates: catalog-pub-v1.0.0.json
```

HTTP headers for caching:

```rust
async fn serve_catalog() -> impl IntoResponse {
    let catalog = include_str!("../target/catalog/catalog-pub-v1.0.0.json");
    
    (
        StatusCode::OK,
        [
            (header::CONTENT_TYPE, "application/json"),
            (header::CACHE_CONTROL, "public, max-age=86400, immutable"),
            (header::ETAG, "v1.0.0"),
        ],
        catalog
    )
}
```

### Differential Updates

For large catalogs, send only changes:

```typescript
interface CatalogDiff {
  added: Record<string, CatalogEntry>;
  modified: Record<string, CatalogEntry>;
  removed: string[];
}

async function updateCatalog(currentVersion: string): Promise<void> {
  const response = await fetch(`/catalogs/diff/${currentVersion}/latest`);
  const diff: CatalogDiff = await response.json();
  
  // Apply diff to cached catalog
  const catalog = getCachedCatalog();
  Object.assign(catalog.e, diff.added);
  Object.assign(catalog.e, diff.modified);
  diff.removed.forEach(hash => delete catalog.e[hash]);
  
  saveCatalog(catalog);
}
```

---

## Best Practices

### DO โœ…

1. **Version your catalogs** - Include version in filename/URL
2. **Compress responses** - Use gzip/brotli compression
3. **Cache aggressively** - Catalogs rarely change
4. **Validate hashes** - Ensure hash exists before expanding
5. **Fallback to hash** - Display hash if catalog missing
6. **Use Compact format** - For production APIs
7. **Generate per-language** - Don't mix languages in one catalog
8. **Include timestamps** - Track when catalog was generated

### DON'T โŒ

1. **Don't send full errors** - Defeats the purpose
2. **Don't skip caching** - Downloads on every request waste bandwidth
3. **Don't use Full format** - Too large for production
4. **Don't mix roles** - Keep Public/Developer/Internal separate
5. **Don't hardcode hashes** - Use hash constants from code
6. **Don't ignore version** - Old clients need compatible catalogs
7. **Don't skip validation** - Malformed catalogs break clients

---

## Monitoring & Analytics

### Track Catalog Usage

```rust
// Server-side metrics
#[derive(Serialize)]
struct CatalogMetrics {
    downloads: u64,
    cache_hits: u64,
    version: String,
    language: String,
}

async fn download_catalog(lang: String, version: String) -> impl IntoResponse {
    // Track metrics
    metrics::increment_counter!("catalog_downloads", "lang" => lang.clone());
    
    // Serve catalog
    let catalog = load_catalog(&lang, &version);
    (StatusCode::OK, Json(catalog))
}
```

### Error Expansion Metrics

```typescript
// Client-side analytics
function expandWithMetrics(hash: string, fields: Record<string, string>): string {
  const start = performance.now();
  
  try {
    const message = catalog.expand(hash, fields);
    
    // Track successful expansion
    analytics.track('error_expanded', {
      hash,
      duration: performance.now() - start,
      success: true
    });
    
    return message;
  } catch (error) {
    // Track failures
    analytics.track('error_expanded', {
      hash,
      duration: performance.now() - start,
      success: false,
      error: error.message
    });
    
    return `Unknown error: ${hash}`;
  }
}
```

---

## Example: Complete IoT System

```rust
// device.rs (IoT device - no_std)
#![no_std]

use waddling_errors::prelude::*;

// Send only hash + fields
pub fn send_error_compact(hash: &str, temp: f32) {
    let payload = format!("{},{}", hash, temp);  // "jGKFp,45.2"
    radio_transmit(payload.as_bytes());  // 12 bytes
}

// server.rs (Server - std)
use waddling_errors::doc_generator::{DocRegistry, CatalogRenderer};

fn expand_device_error(payload: &str) -> String {
    let parts: Vec<&str> = payload.split(',').collect();
    let hash = parts[0];
    let temp = parts[1];
    
    // Load catalog
    let catalog = load_catalog("en");
    
    // Expand error
    catalog.expand(hash, [("temp", temp)].into())
}

fn main() {
    // Generate catalog at build time
    let mut registry = DocRegistry::new("IoTDevice", "1.0.0");
    register_device_errors(&mut registry).unwrap();
    
    let catalog = CatalogRenderer::new(CatalogFormat::Minimal);
    registry.render(vec![Box::new(catalog)], "target/catalog").unwrap();
    
    // Simulate receiving device error
    let device_payload = "jGKFp,45.2";
    let expanded = expand_device_error(device_payload);
    println!("{}", expanded);
    // "Temperature 45.2ยฐC exceeds threshold"
}
```

---

## Conclusion

The Catalog Renderer transforms waddling-errors from a documentation tool into a network-efficient error communication system. By separating error metadata (catalog) from error instances (hash + fields), you achieve:

- **Massive bandwidth savings** (80%+)
- **Multi-language support** without code changes
- **Offline-first capabilities**
- **Consistent error codes** across all platforms

This makes waddling-errors ideal for:
- IoT and embedded systems
- Mobile applications
- Microservices architectures
- Offline-first PWAs
- Multi-language applications

For more information, see:
- [DOC_GENERATION_GUIDE.md]DOC_GENERATION_GUIDE.md - Full documentation generation workflow
- [examples/browser_server_catalog.rs]../../waddling-errors-macros/examples/browser_server_catalog.rs - Complete working example
- [API Documentation]https://docs.rs/waddling-errors - Full API reference