|
| 1 | +//! Fatal error detection for StackQL query execution. |
| 2 | +//! |
| 3 | +//! Maintains a list of error patterns that indicate unrecoverable failures |
| 4 | +//! (network issues, auth failures, etc.) vs normal operational errors |
| 5 | +//! (404 not found) that the retry/statecheck logic can handle. |
| 6 | +
|
| 7 | +/// Error patterns that indicate a fatal, non-retryable failure. |
| 8 | +/// |
| 9 | +/// These are checked against the error message string returned by the |
| 10 | +/// StackQL engine. If any pattern matches, the operation is aborted |
| 11 | +/// immediately rather than retried. |
| 12 | +/// |
| 13 | +/// Two categories: |
| 14 | +/// |
| 15 | +/// 1. **Network errors** - The request never reached the API. Any result |
| 16 | +/// from a query in this state is untrustworthy (e.g., an exists check |
| 17 | +/// returning empty could cause a duplicate resource to be created). |
| 18 | +/// |
| 19 | +/// 2. **HTTP status errors** - The request reached the API but the response |
| 20 | +/// indicates an unrecoverable problem (auth failure, forbidden, etc.). |
| 21 | +/// 404 is explicitly excluded as it's normal for exists checks. |
| 22 | +const FATAL_ERROR_PATTERNS: &[&str] = &[ |
| 23 | + // Network-layer errors (Go net/http) |
| 24 | + "dial tcp:", |
| 25 | + "Client.Timeout exceeded", |
| 26 | + "connection refused", |
| 27 | + "no such host", |
| 28 | + "request canceled while waiting for connection", |
| 29 | + "request canceled (Client.Timeout", |
| 30 | + "tls: handshake", |
| 31 | + "certificate", |
| 32 | + "network is unreachable", |
| 33 | + "connection reset by peer", |
| 34 | + "broken pipe", |
| 35 | + "EOF", |
| 36 | + // HTTP status codes that are never retryable |
| 37 | + "http response status code: 401", |
| 38 | + "http response status code: 403", |
| 39 | +]; |
| 40 | + |
| 41 | +/// Patterns that indicate a non-fatal error, even if a fatal pattern |
| 42 | +/// also matches. These take precedence over `FATAL_ERROR_PATTERNS`. |
| 43 | +/// |
| 44 | +/// For example, a 404 is normal for exists checks on resources that |
| 45 | +/// don't exist yet. |
| 46 | +const NON_FATAL_OVERRIDES: &[&str] = &[ |
| 47 | + "http response status code: 404", |
| 48 | + "ResourceNotFoundException", |
| 49 | + "was not found", |
| 50 | +]; |
| 51 | + |
| 52 | +/// Check if an error message indicates a fatal, non-retryable failure. |
| 53 | +/// |
| 54 | +/// Returns `Some(reason)` if the error is fatal, `None` if it's |
| 55 | +/// a normal operational error that can be retried or handled. |
| 56 | +pub fn check_fatal_error(error_msg: &str) -> Option<&'static str> { |
| 57 | + // First check if any non-fatal override matches |
| 58 | + for pattern in NON_FATAL_OVERRIDES { |
| 59 | + if error_msg.contains(pattern) { |
| 60 | + return None; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Then check for fatal patterns |
| 65 | + for pattern in FATAL_ERROR_PATTERNS { |
| 66 | + if error_msg.contains(pattern) { |
| 67 | + return Some(pattern); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + None |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use super::*; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_network_timeout_is_fatal() { |
| 80 | + let msg = r#"Query execution failed: query returns error: Post "https://cloudcontrolapi.us-east-1.amazonaws.com/?Action=GetResource&Version=2021-09-30": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"#; |
| 81 | + assert!(check_fatal_error(msg).is_some()); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_dns_failure_is_fatal() { |
| 86 | + let msg = r#"Query execution failed: query returns error: Post "https://cloudcontrolapi.us-east-1.amazonaws.com/": dial tcp: lookup cloudcontrolapi.us-east-1.amazonaws.com on 8.8.4.4:53: i/o timeout"#; |
| 87 | + assert!(check_fatal_error(msg).is_some()); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_403_is_fatal() { |
| 92 | + let msg = r#"http response status code: 403, response body: {"message":"Access Denied"}"#; |
| 93 | + assert!(check_fatal_error(msg).is_some()); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_401_is_fatal() { |
| 98 | + let msg = r#"http response status code: 401, response body: {"message":"Unauthorized"}"#; |
| 99 | + assert!(check_fatal_error(msg).is_some()); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_404_is_not_fatal() { |
| 104 | + let msg = r#"http response status code: 404, response body: {"__type":"ResourceNotFoundException","Message":"Resource not found"}"#; |
| 105 | + assert!(check_fatal_error(msg).is_none()); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_resource_not_found_is_not_fatal() { |
| 110 | + let msg = r#"Resource of type 'AWS::EC2::VPC' with identifier 'vpc-xxx' was not found"#; |
| 111 | + assert!(check_fatal_error(msg).is_none()); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_400_bad_request_is_not_fatal() { |
| 116 | + let msg = r#"insert over HTTP error: 400 Bad Request"#; |
| 117 | + assert!(check_fatal_error(msg).is_none()); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_normal_query_error_is_not_fatal() { |
| 122 | + let msg = r#"query returns error: no such column: foo"#; |
| 123 | + assert!(check_fatal_error(msg).is_none()); |
| 124 | + } |
| 125 | +} |
0 commit comments