1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2026-05-30 04:07:33 +00:00

Detect Too Many Requests descriptions case-insensitively

Telegram has been observed to return both "Bad Request: Too Many
Requests: retry after N" and "Bad Request: too Many Requests: retry
after N" (lowercase leading 't'). The current case-sensitive check
silently misses the latter and the caller never sees a
TooMuchRequestsException.

Added `ignoreCase = true` to the contains(...) check and three
regression tests covering canonical, lowercase-initial, and
all-lowercase casings.

Closes #1008
This commit is contained in:
emad
2026-04-19 03:24:16 +03:00
parent 16e94a1e1b
commit 4500711db4
2 changed files with 40 additions and 1 deletions

View File

@@ -22,7 +22,7 @@ fun newRequestException(
description == "Unauthorized" -> UnauthorizedException(response, plainAnswer, message, cause)
description.contains("PHOTO_INVALID_DIMENSIONS") -> InvalidPhotoDimensionsException(response, plainAnswer, message, cause)
description.contains("wrong file identifier") -> WrongFileIdentifierException(response, plainAnswer, message, cause)
description.contains("Too Many Requests") -> TooMuchRequestsException(
description.contains("Too Many Requests", ignoreCase = true) -> TooMuchRequestsException(
(response.parameters ?.error as? RetryAfterError) ?: RetryAfterError(60, DateTime.now().unixMillisLong),
response,
plainAnswer,

View File

@@ -0,0 +1,39 @@
package dev.inmo.tgbotapi.bot.exceptions
import dev.inmo.tgbotapi.types.Response
import kotlin.test.Test
import kotlin.test.assertIs
/**
* Regression tests for https://github.com/InsanusMokrassar/TelegramBotAPI/issues/1008 —
* Telegram's "Too Many Requests" response is case-inconsistent (both
* `Too Many Requests` and `too Many Requests` have been observed), so the
* description match must be case-insensitive.
*/
class NewRequestExceptionTests {
private fun buildException(description: String) = newRequestException(
response = Response(ok = false, description = description, errorCode = 429),
plainAnswer = "{\"ok\":false}"
)
@Test
fun `TooMuchRequestsException is created for canonical casing`() {
assertIs<TooMuchRequestsException>(
buildException("Bad Request: Too Many Requests: retry after 8")
)
}
@Test
fun `TooMuchRequestsException is created for lowercase first-letter casing`() {
assertIs<TooMuchRequestsException>(
buildException("Bad Request: too Many Requests: retry after 8")
)
}
@Test
fun `TooMuchRequestsException is created for all-lowercase casing`() {
assertIs<TooMuchRequestsException>(
buildException("Bad Request: too many requests: retry after 8")
)
}
}