address review: expand 402 regex, fix live-auth-keys, add tests

- Add verb patterns (got/returned/received) to catch real billing errors
- Fix same broad `402` match in live-auth-keys.ts (reviewer point #3)
- Add false-positive tests for plain numeric '402' (point #1)
- Add true-positive tests for 'got a 402 from the API' etc. (point #2)
This commit is contained in:
0xRaini
2026-02-11 20:02:34 +08:00
committed by Sebastian
parent 4eba2334cf
commit d4899bee1a
3 changed files with 12 additions and 2 deletions

View File

@@ -90,7 +90,11 @@ export function isAnthropicBillingError(message: string): boolean {
if (lower.includes("billing") && lower.includes("disabled")) {
return true;
}
if (lower.includes("402")) {
if (
/(?:status|code|http|error)\s*[:=]?\s*402\b|(?:got|returned|received|a)\s+(?:a\s+)?402\b|^\s*402\s+payment/i.test(
lower,
)
) {
return true;
}
return false;

View File

@@ -35,6 +35,9 @@ describe("isBillingErrorMessage", () => {
"Room 402 is available",
"Error code 403 was returned, not 402-related",
"The building at 402 Main Street",
"processed 402 records",
"402 items found in the database",
"port 402 is open",
];
for (const sample of falsePositives) {
expect(isBillingErrorMessage(sample)).toBe(false);
@@ -47,6 +50,9 @@ describe("isBillingErrorMessage", () => {
"error code 402",
"http 402",
"status=402 payment required",
"got a 402 from the API",
"returned 402",
"received a 402 response",
];
for (const sample of realErrors) {
expect(isBillingErrorMessage(sample)).toBe(true);

View File

@@ -535,7 +535,7 @@ const ERROR_PATTERNS = {
overloaded: [/overloaded_error|"type"\s*:\s*"overloaded_error"/i, "overloaded"],
timeout: ["timeout", "timed out", "deadline exceeded", "context deadline exceeded"],
billing: [
/(?:status|code|http|error)\s*[:=]?\s*402\b|^\s*402\s+payment/i,
/(?:status|code|http|error)\s*[:=]?\s*402\b|(?:got|returned|received|a)\s+(?:a\s+)?402\b|^\s*402\s+payment/i,
"payment required",
"insufficient credits",
"credit balance",