fix(providers): replace HTTP-specific regex with standalone status code matcher
The precompiled HTTP regex used uppercase "HTTP" which never matched because ClassifyError lowercases the input. Replace it with a case-insensitive word-boundary pattern that matches any standalone 3-digit status code (300-599), which also subsumes the HTTP/x.x case. Add test case for standalone status code extraction.
This commit is contained in:
parent
7e55680c6b
commit
a45408ff67
2 changed files with 3 additions and 2 deletions
|
|
@ -9,7 +9,7 @@ import (
|
||||||
// Common patterns in Go HTTP error messages
|
// Common patterns in Go HTTP error messages
|
||||||
var httpStatusPatterns = []*regexp.Regexp{
|
var httpStatusPatterns = []*regexp.Regexp{
|
||||||
regexp.MustCompile(`status[:\s]+(\d{3})`),
|
regexp.MustCompile(`status[:\s]+(\d{3})`),
|
||||||
regexp.MustCompile(`HTTP[/\s]+\d*\.?\d*\s+(\d{3})`),
|
regexp.MustCompile(`\b([3-5]\d{2})\b`),
|
||||||
}
|
}
|
||||||
|
|
||||||
// errorPattern defines a single pattern (string or regex) for error classification.
|
// errorPattern defines a single pattern (string or regex) for error classification.
|
||||||
|
|
@ -204,7 +204,7 @@ func classifyByMessage(msg string) FailoverReason {
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractHTTPStatus extracts an HTTP status code from an error message.
|
// extractHTTPStatus extracts an HTTP status code from an error message.
|
||||||
// Looks for patterns like "status: 429", "status 429", "HTTP 429", or standalone "429".
|
// Looks for patterns like "status: 429", "status 429", "HTTP/1.1 429", "HTTP 429", or standalone "429".
|
||||||
func extractHTTPStatus(msg string) int {
|
func extractHTTPStatus(msg string) int {
|
||||||
for _, p := range httpStatusPatterns {
|
for _, p := range httpStatusPatterns {
|
||||||
if m := p.FindStringSubmatch(msg); len(m) > 1 {
|
if m := p.FindStringSubmatch(msg); len(m) > 1 {
|
||||||
|
|
|
||||||
|
|
@ -306,6 +306,7 @@ func TestExtractHTTPStatus(t *testing.T) {
|
||||||
{"status: 429 rate limited", 429},
|
{"status: 429 rate limited", 429},
|
||||||
{"status 401 unauthorized", 401},
|
{"status 401 unauthorized", 401},
|
||||||
{"HTTP/1.1 502 Bad Gateway", 502},
|
{"HTTP/1.1 502 Bad Gateway", 502},
|
||||||
|
{"error 429", 429},
|
||||||
{"no status code here", 0},
|
{"no status code here", 0},
|
||||||
{"random number 12345", 0},
|
{"random number 12345", 0},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue