1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/exceptions/RequestException.kt

48 lines
2.3 KiB
Kotlin
Raw Normal View History

2019-02-10 05:41:20 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
2020-03-22 07:37:01 +00:00
import io.ktor.utils.io.errors.IOException
2019-02-10 05:41:20 +00:00
fun newRequestException(
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 {
description == "Bad Request: reply message not found" -> ReplyMessageNotFoundException(response, plainAnswer, message, cause)
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)
2019-12-13 17:34:58 +00:00
else -> null
}
} ?: CommonRequestException(response, plainAnswer, message, cause)
2019-02-10 05:41:20 +00:00
2019-03-06 00:10:29 +00:00
sealed class RequestException constructor(
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,
2020-01-15 06:28:06 +00:00
override val cause: Throwable? = null
2019-02-10 05:41:20 +00:00
) : IOException(
2020-01-15 06:28:06 +00:00
message ?: "Something went wrong"
2019-03-06 00:10:29 +00:00
)
class CommonRequestException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
2019-03-06 00:10:29 +00:00
RequestException(response, plainAnswer, message, cause)
class UnauthorizedException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
2019-03-06 00:10:29 +00:00
RequestException(response, plainAnswer, message, cause)
class ReplyMessageNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
2019-03-06 00:10:29 +00:00
RequestException(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?) :
2019-12-13 17:34:58 +00:00
RequestException(response, plainAnswer, message, cause)
class MessageToEditNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
RequestException(response, plainAnswer, message, cause)
2020-04-05 08:06:40 +00:00
class InvalidPhotoDimensionsException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
RequestException(response, plainAnswer, message, cause)