diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab914a..cb649b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- **`TangoRateLimitError.limit_type` returns `premium_daily` for a per-feature cap instead of `daily`.** A detail that names no rate is now classified only by the words "daily" or "burst", never by "minutes" or "day" in its wait time. + ## [1.21.0] - 2026-10-29 ### Added diff --git a/tango/exceptions.py b/tango/exceptions.py index 52ccae7..b5f74a9 100644 --- a/tango/exceptions.py +++ b/tango/exceptions.py @@ -5,6 +5,7 @@ # The API names the tripped rate in its 429 detail: "Rate limit exceeded for daily. Please try again in 16 hours, 58 minutes." _RATE_NAME = re.compile(r"exceeded for ([\w-]+)", re.IGNORECASE) +_LIMIT_TYPES = {"daily": "daily", "burst": "burst", "premium_daily": "premium_daily"} class TangoAPIError(Exception): @@ -88,16 +89,22 @@ def detail(self) -> str | None: @property def limit_type(self) -> str | None: - """Which limit was hit: 'burst' or 'daily', parsed from detail.""" + """Which limit was hit, parsed from detail. + + `"daily"` or `"burst"` for the account's own caps, or `"premium_daily"` for a per-feature daily cap + (currently the CALC labor-rate endpoints). `None` when the detail names no limit the SDK recognizes. + """ d = self.detail if not d: return None - # Read the rate's name, not the whole sentence: a daily lockout's wait reads "16 hours, 58 minutes", which a match on "minute" took for burst. match = _RATE_NAME.search(d) - text = match.group(1).lower() if match else d.lower() - if "daily" in text or re.search(r"\bday\b", text): + if match: + return _LIMIT_TYPES.get(match.group(1).lower()) + # Exact words only: "minute" and "day" also appear in the wait text ("9 hours, 14 minutes"), so they can't identify the limit. + lower = d.lower() + if re.search(r"\bdaily\b", lower): return "daily" - if "burst" in text or "minute" in text: + if re.search(r"\bburst\b", lower): return "burst" return None diff --git a/tests/test_client.py b/tests/test_client.py index c2e9384..b039e1f 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1507,9 +1507,23 @@ def test_429_daily_limit_error(self, mock_request): [ # The API's own wording, with its waits as convert_seconds_to_human_readable_wait_time renders them. ("Rate limit exceeded for daily. Please try again in 16 hours, 58 minutes.", "daily"), + ( + "Rate limit exceeded for daily. Please try again in 9 hours, 14 minutes, 3 seconds.", + "daily", + ), ("Rate limit exceeded for daily. Please try again in 1 minute.", "daily"), - ("Rate limit exceeded for burst. Please try again in 45 seconds.", "burst"), + ("Rate limit exceeded for burst. Please try again in 41 seconds.", "burst"), ("Rate limit exceeded for burst. Please try again in 1 minute.", "burst"), + ( + "Rate limit exceeded for premium_daily. Please try again in 9 hours, 14 minutes, 3 seconds.", + "premium_daily", + ), + ( + "Rate limit exceeded for daily. Please try again in 9 hours, 14 minutes, 3 seconds. " + "This limit is shared by everyone on your team; https://tango.makegov.com/account/team/ shows who to contact.", + "daily", + ), + ("Rate limit exceeded for monthly. Please try again in 3 days.", None), ("Too many requests.", None), ], ) @@ -1518,6 +1532,22 @@ def test_limit_type_reads_the_rate_name_not_the_wait(self, detail, expected): assert error.limit_type == expected + @pytest.mark.parametrize( + ("detail", "expected"), + [ + ("Daily request quota reached.", "daily"), + ("Burst limit reached.", "burst"), + ("Request was throttled. Expected available in 3 minutes.", None), + ("Request was throttled. Expected available in 1 day.", None), + ], + ) + def test_limit_type_without_a_rate_name_matches_only_the_words_daily_and_burst( + self, detail, expected + ): + error = TangoRateLimitError("Rate limit exceeded", 429, {"detail": detail}) + + assert error.limit_type == expected + @patch("tango.client.httpx.Client.request") def test_429_empty_body(self, mock_request): """Test 429 with no content body still works"""