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(
|
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 {
|
|
|
|
description == "Bad Request: reply 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)
|
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(
|
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,
|
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
|
|
|
)
|
|
|
|
|
2019-08-17 05:50:46 +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)
|
|
|
|
|
2019-08-17 05:50:46 +00:00
|
|
|
class UnauthorizedException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
2019-03-06 00:10:29 +00:00
|
|
|
RequestException(response, plainAnswer, message, cause)
|
|
|
|
|
2019-08-17 05:50:46 +00:00
|
|
|
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)
|
2019-12-13 17:55:06 +00:00
|
|
|
|
|
|
|
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)
|