apollo-errors 0.7.0

Structured error handling with automatic format conversion
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
//! Tests for transparent variant support
use apollo_errors::{CodeCase, FieldCase, FormatConfig};

mod common;

use apollo_errors::Error as ErrorTrait;
use common::{
    ConfigStructError, InnerError, SimpleStructError, TransparentToStructError,
    TransparentWrapperError,
};
use http::StatusCode;
use insta::assert_json_snapshot;

// ============================================================================
// Display trait tests
// ============================================================================

#[test]
fn test_transparent_display_forwards_to_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    assert_eq!(error.to_string(), "Database connection failed");
}

#[test]
fn test_transparent_display_with_fields() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 5000 });
    assert_eq!(error.to_string(), "Network timeout after 5000ms");
}

// ============================================================================
// Error::source tests
// ============================================================================

#[test]
fn test_transparent_source_returns_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    let source = std::error::Error::source(&error);
    assert!(source.is_some());
    assert_eq!(source.unwrap().to_string(), "Database connection failed");
}

// ============================================================================
// JSON format tests
// ============================================================================

#[test]
fn test_transparent_json_forwards_to_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "db::connection_failed",
      "message": "Database connection failed"
    }
    "#);
}

#[test]
fn test_transparent_json_with_extension_fields() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 5000 });
    let json = error.to_json(FormatConfig::default()).unwrap();
    // Note: JSON uses the raw message template, fields are provided separately
    assert_json_snapshot!(json, @r#"
    {
      "error": "network::timeout",
      "message": "Network timeout after 5000ms",
      "timeout_ms": 5000
    }
    "#);
}

// ============================================================================
// GraphQL format tests
// ============================================================================

#[test]
fn test_transparent_graphql_forwards_to_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    let graphql = error.to_graphql(FormatConfig::default()).unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "db::connection_failed"
      },
      "message": "Database connection failed"
    }
    "#);
}

#[test]
fn test_transparent_graphql_with_extension_fields() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 3000 });
    let graphql = error.to_graphql(FormatConfig::default()).unwrap();
    // Note: GraphQL uses the raw message template, fields are in extensions
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "network::timeout",
        "timeout_ms": 3000
      },
      "message": "Network timeout after 3000ms"
    }
    "#);
}

// ============================================================================
// JSON-RPC format tests
// ============================================================================

#[test]
fn test_transparent_jsonrpc_forwards_to_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    let jsonrpc = error.to_jsonrpc(FormatConfig::default()).unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32000,
      "data": {
        "diagnostic_code": "db::connection_failed"
      },
      "message": "Database connection failed"
    }
    "#);
}

#[test]
fn test_transparent_jsonrpc_with_extension_fields() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 5000 });
    let jsonrpc = error.to_jsonrpc(FormatConfig::default()).unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32000,
      "data": {
        "diagnostic_code": "network::timeout",
        "timeout_ms": 5000
      },
      "message": "Network timeout after 5000ms"
    }
    "#);
}

// ============================================================================
// HTTP status tests
// ============================================================================

#[test]
fn test_transparent_http_status_forwards_to_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    assert_eq!(error.http_status(), StatusCode::SERVICE_UNAVAILABLE);
}

#[test]
fn test_transparent_http_status_with_different_status() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 1000 });
    assert_eq!(error.http_status(), StatusCode::GATEWAY_TIMEOUT);
}

// ============================================================================
// Text format tests
// ============================================================================

#[test]
fn test_transparent_text_forwards_to_inner() {
    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    assert_eq!(
        error.to_text(FormatConfig::default()),
        "[db::connection_failed] Database connection failed"
    );
}

// ============================================================================
// Diagnostic trait tests
// ============================================================================

#[test]
fn test_transparent_diagnostic_code_forwards_to_inner() {
    use miette::Diagnostic;

    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    let code = error.code().map(|c| c.to_string());
    assert_eq!(code, Some("db::connection_failed".to_string()));
}

#[test]
fn test_transparent_diagnostic_help_forwards_to_inner() {
    use miette::Diagnostic;

    let error = TransparentWrapperError::Inner(InnerError::DatabaseError);
    let help = error.help().map(|h| h.to_string());
    assert_eq!(help, Some("Check your database credentials".to_string()));
}

// ============================================================================
// Regular variant still works
// ============================================================================

#[test]
fn test_regular_variant_in_mixed_enum_still_works() {
    let error = TransparentWrapperError::ApplicationError;
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "app::error",
      "message": "Application error"
    }
    "#);
}

// ============================================================================
// Transparent variant wrapping struct tests
// ============================================================================

#[test]
fn test_transparent_to_struct_display() {
    let error = TransparentToStructError::Simple(SimpleStructError);
    assert_eq!(error.to_string(), "Simple struct error occurred");
}

#[test]
fn test_transparent_to_struct_with_fields_display() {
    let error = TransparentToStructError::Config(ConfigStructError {
        port: 9999,
        config_path: "/app/config.yaml".to_string(),
    });
    assert_eq!(error.to_string(), "Configuration error: invalid port 9999");
}

#[test]
fn test_transparent_to_struct_json() {
    let error = TransparentToStructError::Simple(SimpleStructError);
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "structs::simple",
      "message": "Simple struct error occurred"
    }
    "#);
}

#[test]
fn test_transparent_to_struct_with_fields_json() {
    let error = TransparentToStructError::Config(ConfigStructError {
        port: 8080,
        config_path: "/etc/app.toml".to_string(),
    });
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_path": "/etc/app.toml",
      "error": "structs::config_error",
      "message": "Configuration error: invalid port 8080",
      "port": 8080
    }
    "#);
}

#[test]
fn test_transparent_to_struct_graphql() {
    let error = TransparentToStructError::Config(ConfigStructError {
        port: 443,
        config_path: "/config.json".to_string(),
    });
    let graphql = error.to_graphql(FormatConfig::default()).unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "structs::config_error",
        "config_path": "/config.json",
        "port": 443
      },
      "message": "Configuration error: invalid port 443"
    }
    "#);
}

#[test]
fn test_transparent_to_struct_jsonrpc() {
    let error = TransparentToStructError::Config(ConfigStructError {
        port: 443,
        config_path: "/config.json".to_string(),
    });
    let jsonrpc = error.to_jsonrpc(FormatConfig::default()).unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32000,
      "data": {
        "config_path": "/config.json",
        "diagnostic_code": "structs::config_error",
        "port": 443
      },
      "message": "Configuration error: invalid port 443"
    }
    "#);
}

#[test]
fn test_transparent_to_struct_http_status() {
    // SimpleStructError uses default 500
    let error = TransparentToStructError::Simple(SimpleStructError);
    assert_eq!(error.http_status(), StatusCode::INTERNAL_SERVER_ERROR);
}

#[test]
fn test_transparent_to_struct_custom_http_status() {
    // ConfigStructError has custom 400
    let error = TransparentToStructError::Config(ConfigStructError {
        port: 80,
        config_path: "config".to_string(),
    });
    assert_eq!(error.http_status(), StatusCode::BAD_REQUEST);
}

#[test]
fn test_transparent_to_struct_text() {
    let error = TransparentToStructError::Simple(SimpleStructError);
    assert_eq!(
        error.to_text(FormatConfig::default()),
        "[structs::simple] Simple struct error occurred"
    );
}

#[test]
fn test_transparent_to_struct_diagnostic_code() {
    use miette::Diagnostic;

    let error = TransparentToStructError::Config(ConfigStructError {
        port: 80,
        config_path: "config".to_string(),
    });
    let code = error.code().map(|c| c.to_string());
    assert_eq!(code, Some("structs::config_error".to_string()));
}

#[test]
fn test_transparent_to_struct_diagnostic_help() {
    use miette::Diagnostic;

    let error = TransparentToStructError::Config(ConfigStructError {
        port: 80,
        config_path: "config".to_string(),
    });
    let help = error.help().map(|h| h.to_string());
    assert_eq!(help, Some("Check your configuration file".to_string()));
}

#[test]
fn test_transparent_to_struct_source_returns_inner() {
    let error = TransparentToStructError::Config(ConfigStructError {
        port: 80,
        config_path: "config".to_string(),
    });
    let source = std::error::Error::source(&error);
    assert!(source.is_some());
    assert_eq!(
        source.unwrap().to_string(),
        "Configuration error: invalid port 80"
    );
}

#[test]
fn test_direct_variant_in_transparent_to_struct_enum() {
    let error = TransparentToStructError::Direct;
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "wrapper::direct",
      "message": "Direct enum error"
    }
    "#);
}

// ============================================================================
// FormatConfig propagation through transparent variants
// ============================================================================

#[test]
fn test_transparent_propagates_field_case() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 5000 });
    let config = FormatConfig {
        field_case: FieldCase::CamelCase,
        ..FormatConfig::default()
    };
    let json = error.to_json(config).unwrap();
    let obj = json.as_object().unwrap();
    assert!(
        obj.contains_key("timeoutMs"),
        "transparent delegation should apply field_case, got keys: {:?}",
        obj.keys().collect::<Vec<_>>()
    );
    assert!(!obj.contains_key("timeout_ms"));
}

#[test]
fn test_transparent_propagates_code_case() {
    let error = TransparentWrapperError::Inner(InnerError::NetworkTimeout { timeout_ms: 1000 });
    let config = FormatConfig {
        code_case: CodeCase::ScreamingSnakeCase,
        ..FormatConfig::default()
    };
    let graphql = error.to_graphql(config).unwrap();
    assert_eq!(
        graphql["extensions"]["code"].as_str().unwrap(),
        "NETWORK_TIMEOUT"
    );
}