2020-10-04 11:06:30 +00:00
|
|
|
package dev.inmo.tgbotapi.bot.exceptions
|
2019-02-10 05:41:20 +00:00
|
|
|
|
2023-05-27 12:19:14 +00:00
|
|
|
import korlibs.time.DateTime
|
2021-01-09 14:59:37 +00:00
|
|
|
import dev.inmo.tgbotapi.types.Response
|
|
|
|
import dev.inmo.tgbotapi.types.RetryAfterError
|
2020-03-22 07:37:01 +00:00
|
|
|
import io.ktor.utils.io.errors.IOException
|
2022-09-18 19:39:23 +00:00
|
|
|
import kotlinx.coroutines.CopyableThrowable
|
2019-02-10 05:41:20 +00:00
|
|
|
|
|
|
|
fun newRequestException(
|
2019-08-17 05:50:46 +00:00
|
|
|
response: Response,
|
2019-03-04 02:32:26 +00:00
|
|
|
plainAnswer: String,
|
2019-02-10 05:41:20 +00:00
|
|
|
message: String? = null,
|
|
|
|
cause: Throwable? = null
|
2019-12-13 17:34:58 +00:00
|
|
|
) = response.description ?.let { description ->
|
|
|
|
when {
|
2021-03-26 01:03:33 +00:00
|
|
|
description == "Bad Request: reply message not found" || description == "Bad Request: replied message not found" -> ReplyMessageNotFoundException(response, plainAnswer, message, cause)
|
2019-12-13 17:55:06 +00:00
|
|
|
description == "Bad Request: message to edit not found" -> MessageToEditNotFoundException(response, plainAnswer, message, cause)
|
2019-12-13 17:36:31 +00:00
|
|
|
description.contains("Bad Request: message is not modified") -> MessageIsNotModifiedException(response, plainAnswer, message, cause)
|
2019-12-13 17:34:58 +00:00
|
|
|
description == "Unauthorized" -> UnauthorizedException(response, plainAnswer, message, cause)
|
2020-04-05 08:06:40 +00:00
|
|
|
description.contains("PHOTO_INVALID_DIMENSIONS") -> InvalidPhotoDimensionsException(response, plainAnswer, message, cause)
|
2020-11-02 07:25:40 +00:00
|
|
|
description.contains("wrong file identifier") -> WrongFileIdentifierException(response, plainAnswer, message, cause)
|
2020-11-11 04:23:20 +00:00
|
|
|
description.contains("Too Many Requests") -> TooMuchRequestsException(
|
|
|
|
(response.parameters ?.error as? RetryAfterError) ?: RetryAfterError(60, DateTime.now().unixMillisLong),
|
|
|
|
response,
|
|
|
|
plainAnswer,
|
|
|
|
message,
|
|
|
|
cause
|
2021-06-30 08:01:59 +00:00
|
|
|
)
|
2021-06-30 07:57:20 +00:00
|
|
|
description.contains("Conflict: terminated by other getUpdates request") -> GetUpdatesConflict(
|
|
|
|
response,
|
|
|
|
plainAnswer,
|
|
|
|
message,
|
|
|
|
cause
|
2020-11-11 04:23:20 +00:00
|
|
|
)
|
2019-12-13 17:34:58 +00:00
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
} ?: CommonRequestException(response, plainAnswer, message, cause)
|
2019-02-10 05:41:20 +00:00
|
|
|
|
2022-09-18 19:39:23 +00:00
|
|
|
sealed class BotException(message: String = "Something went wrong", cause: Throwable? = null) : IOException(message, cause), CopyableThrowable<BotException>
|
2022-08-25 07:32:12 +00:00
|
|
|
|
2022-09-18 19:39:23 +00:00
|
|
|
class CommonBotException(message: String = "Something went wrong", cause: Throwable? = null) : BotException(message, cause) {
|
|
|
|
override fun createCopy(): BotException = CommonBotException(message!!, cause)
|
|
|
|
}
|
2022-08-25 07:32:12 +00:00
|
|
|
|
2019-03-06 00:10:29 +00:00
|
|
|
sealed class RequestException constructor(
|
2019-08-17 05:50:46 +00:00
|
|
|
val response: Response,
|
2019-03-04 02:32:26 +00:00
|
|
|
val plainAnswer: String,
|
2019-02-10 05:41:20 +00:00
|
|
|
message: String? = null,
|
2022-08-25 07:32:12 +00:00
|
|
|
cause: Throwable? = null
|
|
|
|
) : BotException(
|
|
|
|
message ?: "Something went wrong",
|
|
|
|
cause
|
2019-03-06 00:10:29 +00:00
|
|
|
)
|
|
|
|
|
2019-08-17 05:50:46 +00:00
|
|
|
class CommonRequestException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = CommonRequestException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2019-03-06 00:10:29 +00:00
|
|
|
|
2019-08-17 05:50:46 +00:00
|
|
|
class UnauthorizedException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = UnauthorizedException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2019-03-06 00:10:29 +00:00
|
|
|
|
2019-08-17 05:50:46 +00:00
|
|
|
class ReplyMessageNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = ReplyMessageNotFoundException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2019-12-13 17:34:58 +00:00
|
|
|
|
2019-12-13 17:36:31 +00:00
|
|
|
class MessageIsNotModifiedException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = MessageIsNotModifiedException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2019-12-13 17:55:06 +00:00
|
|
|
|
|
|
|
class MessageToEditNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = MessageToEditNotFoundException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2020-04-05 08:06:40 +00:00
|
|
|
|
|
|
|
class InvalidPhotoDimensionsException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = InvalidPhotoDimensionsException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2020-11-02 07:25:40 +00:00
|
|
|
|
|
|
|
class WrongFileIdentifierException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = WrongFileIdentifierException(response, plainAnswer, message, cause)
|
|
|
|
}
|
2020-11-11 04:23:20 +00:00
|
|
|
|
|
|
|
class TooMuchRequestsException(val retryAfter: RetryAfterError, response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = TooMuchRequestsException(retryAfter, response, plainAnswer, message, cause)
|
|
|
|
}
|
2021-06-30 07:57:20 +00:00
|
|
|
|
|
|
|
class GetUpdatesConflict(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2022-09-18 19:39:23 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause) {
|
|
|
|
override fun createCopy(): BotException = GetUpdatesConflict(response, plainAnswer, message, cause)
|
|
|
|
}
|