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
//! Error extraction utilities for failure tracking.
//!
//! This module provides functions to extract error types and HTTP status codes
//! from error chains for failure categorization.
use anyhow::Error;
use hickory_resolver::net::NetError;
use reqwest::Error as ReqwestError;
use crate::error_handling::{
categorize_reqwest_error, categorize_resolve_error, DnsResolveErrorKind, ErrorType,
};
/// Picks `DnsNsLookupError` / `DnsTxtLookupError` / `DnsMxLookupError` from message (used when we
/// already know the error is DNS-related, e.g. from `NetError`).
fn dns_error_type_from_message(msg: &str) -> ErrorType {
if msg.contains("txt") {
ErrorType::DnsTxtLookupError
} else if msg.contains("mx") || msg.contains("mail") {
ErrorType::DnsMxLookupError
} else if mentions_nameserver_record(msg) {
ErrorType::DnsNsLookupError
} else {
// Record-type unknown: NS is the historical default for enrichment NetErrors.
ErrorType::DnsNsLookupError
}
}
/// True when `msg` refers to NS / nameserver records — not the substring `ns` inside `dns`.
fn mentions_nameserver_record(msg: &str) -> bool {
msg.contains("nameserver")
|| msg.contains(" ns ")
|| msg.contains(" ns:")
|| msg.contains(":ns ")
|| msg.starts_with("ns ")
|| msg.ends_with(" ns")
}
/// Extracts error type from an error chain.
///
/// Uses the shared `categorize_reqwest_error` function for consistency,
/// with light message enhancements for TLS and timeouts. DNS record-type labels
/// (NS/TXT/MX) come from the `NetError` enrichment path — not from reclassifying
/// HTTP connect failures whose chain happens to mention `dns error`.
pub(crate) fn extract_error_type(error: &Error) -> ErrorType {
// Check error chain for DNS resolver errors (predicate-based, no string matching)
for cause in error.chain() {
if let Some(resolve_err) = cause.downcast_ref::<NetError>() {
let kind = categorize_resolve_error(resolve_err);
return match kind {
DnsResolveErrorKind::Timeout => ErrorType::ProcessUrlTimeout,
DnsResolveErrorKind::NxDomain
| DnsResolveErrorKind::NoRecords
| DnsResolveErrorKind::Other => {
dns_error_type_from_message(&resolve_err.to_string().to_lowercase())
}
};
}
}
// Check error chain for reqwest errors
for cause in error.chain() {
if let Some(reqwest_err) = cause.downcast_ref::<ReqwestError>() {
// Trust reqwest's Connect/Timeout/Request classification. A connect
// failure often embeds "dns error" in the chain; that is still a
// connect failure, not an NS/TXT/MX enrichment lookup.
let base_type = categorize_reqwest_error(reqwest_err);
let error_msg = reqwest_err.to_string().to_lowercase();
// Check for TLS errors in message
if error_msg.contains("tls")
|| error_msg.contains("ssl")
|| error_msg.contains("certificate")
|| error_msg.contains("handshake")
{
return ErrorType::TlsCertificateError;
}
// Check for timeout in request/connect errors
if (base_type == ErrorType::HttpRequestRequestError
|| base_type == ErrorType::HttpRequestConnectError)
&& error_msg.contains("timeout")
{
return ErrorType::HttpRequestTimeoutError;
}
// Return the base type (from shared categorization)
return base_type;
}
}
// Check error message for specific patterns (for errors without reqwest::Error)
let msg = error.to_string().to_lowercase();
// Check for HTTP request timeout first (more specific)
if msg.contains("request timeout") || msg.contains("http") && msg.contains("timeout") {
return ErrorType::HttpRequestTimeoutError;
} else if msg.contains("timeout") {
return ErrorType::ProcessUrlTimeout;
}
// HTTP client connect failures that mention "dns error" in the chain are still
// connect errors (A/AAAA during dial), not NS-record enrichment failures.
if msg.contains("connect")
&& (msg.contains("dns")
|| msg.contains("name resolution")
|| msg.contains("failed to resolve"))
{
return ErrorType::HttpRequestConnectError;
}
// Check for DNS errors in error message
if msg.contains("dns") || msg.contains("resolve") || msg.contains("lookup failed") {
// Try to determine which DNS lookup failed
// Check for specific types FIRST (more specific patterns before general ones)
if msg.contains("txt") {
return ErrorType::DnsTxtLookupError;
} else if msg.contains("mx") || msg.contains("mail") {
return ErrorType::DnsMxLookupError;
} else if mentions_nameserver_record(&msg) {
return ErrorType::DnsNsLookupError;
}
// Generic DNS error - default to NS lookup error
return ErrorType::DnsNsLookupError;
}
// Check for TLS errors in error message
if msg.contains("tls")
|| msg.contains("ssl")
|| msg.contains("certificate")
|| msg.contains("handshake")
{
return ErrorType::TlsCertificateError;
}
// Default to other error
ErrorType::HttpRequestOtherError
}
/// Extracts HTTP status code from an error chain.
pub fn extract_http_status(error: &Error) -> Option<u16> {
// First try to downcast the error itself
if let Some(reqwest_err) = error.downcast_ref::<ReqwestError>() {
if let Some(status) = reqwest_err.status() {
return Some(status.as_u16());
}
}
// Then check the chain - iterate through sources
for cause in error.chain() {
if let Some(reqwest_err) = cause.downcast_ref::<ReqwestError>() {
if let Some(status) = reqwest_err.status() {
return Some(status.as_u16());
}
}
}
// If we can't find it via downcast, try to extract from the source chain
// by recursively checking if any source is an anyhow::Error containing reqwest::Error
let mut current: Option<&dyn std::error::Error> = error.source();
while let Some(err) = current {
// Try to get the underlying anyhow::Error if this is wrapped
// We can't directly downcast &dyn Error, but we can check if it's an anyhow error
// by attempting to access it through the chain
if let Some(reqwest_err) = err.downcast_ref::<ReqwestError>() {
if let Some(status) = reqwest_err.status() {
return Some(status.as_u16());
}
}
current = err.source();
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error_handling::ErrorType;
#[tokio::test]
async fn test_extract_error_type_reqwest_dns_error() {
// Test DNS error detection from reqwest error message
// This is critical - DNS errors should be categorized correctly
// Create a reqwest error by attempting an invalid request
// Use a longer timeout to avoid timeout errors, focus on DNS errors
let reqwest_err = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()
.unwrap()
.get("http://invalid-domain-that-does-not-exist-12345.com")
.send()
.await
.unwrap_err();
let error = anyhow::Error::from(reqwest_err);
let error_type = extract_error_type(&error);
// NXDOMAIN / resolve failure during dial is a connect/request/timeout — not
// an NS enrichment lookup. Accept network classes only.
assert!(
matches!(
error_type,
ErrorType::HttpRequestConnectError
| ErrorType::HttpRequestTimeoutError
| ErrorType::HttpRequestRequestError
| ErrorType::HttpRequestOtherError
),
"Expected connect/request/timeout for dial-time DNS failure, got: {:?}",
error_type
);
}
#[test]
fn test_connect_with_dns_error_in_chain_is_connect_not_ns() {
// Live scan shape: reqwest Connect failures embed "dns error" in the chain.
// That must not become DnsNsLookupError (`contains("ns")` matching inside "dns").
let error = anyhow::anyhow!(
"error sending request for url (https://example.invalid/): client error (Connect) \
(error chain: error sending request for url (https://example.invalid/) \
-> client error (Connect) -> dns error -> ...)"
);
assert_eq!(
extract_error_type(&error),
ErrorType::HttpRequestConnectError,
"Connect+dns-error chain must stay a connect failure"
);
}
#[test]
fn test_mentions_nameserver_record_does_not_match_inside_dns() {
assert!(!mentions_nameserver_record("dns error"));
assert!(!mentions_nameserver_record("failed dns lookup"));
assert!(mentions_nameserver_record("nameserver lookup failed"));
assert!(mentions_nameserver_record("ns lookup failed"));
}
#[test]
fn test_extract_error_type_reqwest_dns_txt_error() {
// Test TXT-specific DNS error detection
// This is critical - TXT lookups should be distinguished from NS lookups
// Create error with DNS TXT message pattern
let error = anyhow::anyhow!("DNS TXT lookup failed");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::DnsTxtLookupError);
}
#[test]
fn test_extract_error_type_reqwest_dns_mx_error() {
// Test MX-specific DNS error detection
// This is critical - MX lookups should be distinguished
// Create error with DNS MX message pattern
let error = anyhow::anyhow!("DNS mail exchange lookup failed");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::DnsMxLookupError);
}
#[test]
fn test_extract_error_type_reqwest_tls_error() {
// Test TLS error detection from reqwest error message
// This is critical - TLS errors should be categorized correctly
// Create error with TLS message pattern
let error = anyhow::anyhow!("TLS handshake failed: certificate error");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::TlsCertificateError);
}
#[test]
fn test_extract_error_type_reqwest_ssl_error() {
// Test SSL error detection (alternative to TLS)
// Create error with SSL message pattern
let error = anyhow::anyhow!("SSL certificate validation failed");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::TlsCertificateError);
}
#[test]
fn test_extract_error_type_reqwest_timeout_in_message() {
// Test timeout detection when error message contains "timeout"
// This is critical - timeouts should be distinguished from other errors
// Create error with timeout message pattern
let error = anyhow::anyhow!("request timeout after 30 seconds");
let error_type = extract_error_type(&error);
// Should detect timeout from message
assert_eq!(error_type, ErrorType::HttpRequestTimeoutError);
}
#[test]
fn test_extract_error_type_non_reqwest_timeout() {
// Test timeout detection for non-reqwest errors
// This is critical - timeout errors should be detected even without reqwest
let error = anyhow::anyhow!("Process URL timeout after 45 seconds");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::ProcessUrlTimeout);
}
#[test]
fn test_extract_error_type_non_reqwest_dns_error() {
// Test DNS error detection for non-reqwest errors
// This is critical - DNS errors should be detected even without reqwest
let error = anyhow::anyhow!("DNS lookup failed for example.com");
let error_type = extract_error_type(&error);
assert!(
matches!(
error_type,
ErrorType::DnsNsLookupError
| ErrorType::DnsTxtLookupError
| ErrorType::DnsMxLookupError
),
"Expected DNS error type, got: {:?}",
error_type
);
}
#[test]
fn test_extract_error_type_non_reqwest_tls_error() {
// Test TLS error detection for non-reqwest errors
let error = anyhow::anyhow!("TLS certificate validation failed");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::TlsCertificateError);
}
#[test]
fn test_extract_error_type_fallback_to_other() {
// Test fallback to HttpRequestOtherError for unknown errors
// This is critical - unknown errors should not panic
let error = anyhow::anyhow!("Unknown error occurred");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::HttpRequestOtherError);
}
#[tokio::test]
async fn test_extract_http_status_with_status() {
// Test HTTP status extraction from reqwest error
// This is critical - status codes should be extracted correctly
use httptest::{matchers::*, responders::*, Expectation, Server};
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/500"))
.respond_with(status_code(500).body("Internal Server Error")),
);
let client = reqwest::Client::new();
let url = server.url("/500").to_string();
let response = client
.get(&url)
.send()
.await
.expect("Failed to create test request");
let status = response.status();
let reqwest_err = response.error_for_status().unwrap_err();
let error = anyhow::Error::from(reqwest_err);
let extracted_status = extract_http_status(&error);
assert_eq!(extracted_status, Some(status.as_u16()));
}
#[test]
fn test_extract_http_status_no_status() {
// Test HTTP status extraction when no status is available
// This is critical - should return None, not panic
let error = anyhow::anyhow!("Connection error");
let extracted_status = extract_http_status(&error);
assert_eq!(extracted_status, None);
}
#[tokio::test]
async fn test_extract_http_status_nested_error_chain() {
// Test HTTP status extraction from nested error chain
// This is critical - status should be found even if nested
use httptest::{matchers::*, responders::*, Expectation, Server};
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/404"))
.respond_with(status_code(404).body("Not Found")),
);
let client = reqwest::Client::new();
let url = server.url("/404").to_string();
let response = client
.get(&url)
.send()
.await
.expect("Failed to create test request");
let status = response.status();
let reqwest_err = response.error_for_status().unwrap_err();
let error = anyhow::Error::from(reqwest_err)
.context("Additional context")
.context("More context");
let extracted_status = extract_http_status(&error);
assert_eq!(extracted_status, Some(status.as_u16()));
}
#[test]
fn test_extract_error_type_dns_ns_specific() {
// Test that NS-specific DNS errors are detected correctly
// This is critical - NS lookup errors should be distinguished
let error = anyhow::anyhow!("DNS nameserver lookup failed");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::DnsNsLookupError);
}
#[test]
fn test_extract_error_type_dns_generic_falls_back_to_ns() {
// Test that generic DNS errors fall back to NS lookup error
// This is critical - default DNS error type should be NS
let error = anyhow::anyhow!("DNS lookup failed");
let error_type = extract_error_type(&error);
// Generic DNS errors default to NS lookup error (line 83)
assert_eq!(error_type, ErrorType::DnsNsLookupError);
}
#[test]
fn test_extract_error_type_timeout_in_request_error() {
// Test that timeout detection works for request errors
// This is critical - timeouts should be detected even when wrapped in request errors
// The code at line 50-54 checks for timeout in request/connect errors
let error = anyhow::anyhow!("HTTP request error: timeout after 30 seconds");
let error_type = extract_error_type(&error);
// Should detect timeout from message
assert_eq!(error_type, ErrorType::HttpRequestTimeoutError);
}
#[test]
fn test_extract_error_type_tls_certificate_variations() {
// Test that various TLS/SSL error messages are detected
// This is critical - TLS errors can be described in multiple ways
let variations = vec![
"TLS handshake failed",
"SSL certificate error",
"Certificate validation failed",
"TLS certificate expired",
];
for msg in variations {
let error = anyhow::anyhow!(msg);
let error_type = extract_error_type(&error);
assert_eq!(
error_type,
ErrorType::TlsCertificateError,
"Failed to detect TLS error in: {}",
msg
);
}
}
#[test]
fn test_extract_error_type_http_timeout_vs_process_timeout() {
// Test that HTTP timeouts are distinguished from process timeouts
// This is critical - different timeout types should be categorized correctly
// The code at line 65 checks for "request timeout" or "http" + "timeout"
let http_timeout = anyhow::anyhow!("HTTP request timeout");
let process_timeout = anyhow::anyhow!("Process URL timeout after 45 seconds");
let http_type = extract_error_type(&http_timeout);
let process_type = extract_error_type(&process_timeout);
assert_eq!(http_type, ErrorType::HttpRequestTimeoutError);
assert_eq!(process_type, ErrorType::ProcessUrlTimeout);
}
#[test]
fn test_extract_error_type_message_case_insensitive() {
// Test that error message matching is case-insensitive
// This is critical - error messages can be in any case
let variations = vec![
"DNS lookup failed",
"dns lookup failed",
"DNS LOOKUP FAILED",
"DnS lOoKuP fAiLeD",
];
for msg in variations {
let error = anyhow::anyhow!(msg);
let error_type = extract_error_type(&error);
// All should detect DNS error (case-insensitive matching via to_lowercase)
assert!(
matches!(
error_type,
ErrorType::DnsNsLookupError
| ErrorType::DnsTxtLookupError
| ErrorType::DnsMxLookupError
),
"Failed to detect DNS error (case-insensitive) in: {}",
msg
);
}
}
#[test]
fn test_extract_error_type_unknown_fallback_to_other() {
// Test that unknown errors fall back to HttpRequestOtherError
// This is critical - unknown errors should not cause panics
let error = anyhow::anyhow!("Some completely unknown error type");
let error_type = extract_error_type(&error);
assert_eq!(error_type, ErrorType::HttpRequestOtherError);
}
#[tokio::test]
async fn test_extract_http_status_from_error_itself() {
// Test that HTTP status is extracted from error itself first (line 102-105)
// This is critical - direct reqwest errors should be handled efficiently
use httptest::{matchers::*, responders::*, Expectation, Server};
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/503"))
.respond_with(status_code(503).body("Service Unavailable")),
);
let client = reqwest::Client::new();
let url = server.url("/503").to_string();
let response = client
.get(&url)
.send()
.await
.expect("Failed to create test request");
let status = response.status();
let reqwest_err = response.error_for_status().unwrap_err();
let error: anyhow::Error = reqwest_err.into();
// Should extract status from error itself (not just chain)
let extracted_status = extract_http_status(&error);
assert_eq!(extracted_status, Some(status.as_u16()));
}
#[test]
fn test_extract_http_status_no_reqwest_error() {
// Test that non-reqwest errors return None
// This is critical - should handle non-HTTP errors gracefully
let error = anyhow::anyhow!("Database connection error");
let extracted_status = extract_http_status(&error);
assert_eq!(extracted_status, None);
}
}