1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2026-06-05 23:27:22 +00:00

add api exception (solution of #1048) + add classcasts for exceptions

This commit is contained in:
2026-05-28 19:05:53 +06:00
parent cd56c7e257
commit 643133a87b
5 changed files with 213 additions and 15 deletions

View File

@@ -275,16 +275,42 @@ public final class dev/inmo/tgbotapi/bot/RequestsExecutor$DefaultImpls {
public static fun getLog (Ldev/inmo/tgbotapi/bot/RequestsExecutor;)Ldev/inmo/kslog/common/KSLog;
}
public abstract class dev/inmo/tgbotapi/bot/exceptions/BotException : java/io/IOException, kotlinx/coroutines/CopyableThrowable {
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final class dev/inmo/tgbotapi/bot/exceptions/ApiException : dev/inmo/tgbotapi/bot/exceptions/CommonBotException {
public fun <init> (Ljava/lang/Integer;Ljava/lang/String;Ldev/inmo/tgbotapi/types/Response;)V
public synthetic fun <init> (Ljava/lang/Integer;Ljava/lang/String;Ldev/inmo/tgbotapi/types/Response;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun createCopy ()Ldev/inmo/tgbotapi/bot/exceptions/ApiException;
public synthetic fun createCopy ()Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException;
public synthetic fun createCopy ()Ljava/lang/Throwable;
public final fun getHttpResponseCode ()Ljava/lang/Integer;
public final fun getPlainResponse ()Ljava/lang/String;
}
public final class dev/inmo/tgbotapi/bot/exceptions/CommonBotException : dev/inmo/tgbotapi/bot/exceptions/BotException {
public abstract class dev/inmo/tgbotapi/bot/exceptions/BotException : java/io/IOException, kotlinx/coroutines/CopyableThrowable {
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getMessage ()Ljava/lang/String;
public fun getPlainAnswer ()Ljava/lang/String;
public fun getResponse ()Ldev/inmo/tgbotapi/types/Response;
}
public abstract class dev/inmo/tgbotapi/bot/exceptions/CommonBotException : dev/inmo/tgbotapi/bot/exceptions/BotException {
public static final field Companion Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException$Companion;
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public abstract fun createCopy ()Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException;
}
public final class dev/inmo/tgbotapi/bot/exceptions/CommonBotException$Companion {
public final fun invoke (Ljava/lang/String;Ljava/lang/Throwable;)Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException$Default;
public static synthetic fun invoke$default (Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException$Companion;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException$Default;
}
public final class dev/inmo/tgbotapi/bot/exceptions/CommonBotException$Default : dev/inmo/tgbotapi/bot/exceptions/CommonBotException {
public fun <init> ()V
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun createCopy ()Ldev/inmo/tgbotapi/bot/exceptions/BotException;
public fun createCopy ()Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException$Default;
public synthetic fun createCopy ()Ldev/inmo/tgbotapi/bot/exceptions/CommonBotException;
public synthetic fun createCopy ()Ljava/lang/Throwable;
}
@@ -327,13 +353,13 @@ public final class dev/inmo/tgbotapi/bot/exceptions/ReplyMessageNotFoundExceptio
public abstract class dev/inmo/tgbotapi/bot/exceptions/RequestException : dev/inmo/tgbotapi/bot/exceptions/BotException {
public synthetic fun <init> (Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getPlainAnswer ()Ljava/lang/String;
public final fun getResponse ()Ldev/inmo/tgbotapi/types/Response;
public fun getPlainAnswer ()Ljava/lang/String;
public fun getResponse ()Ldev/inmo/tgbotapi/types/Response;
}
public final class dev/inmo/tgbotapi/bot/exceptions/RequestExceptionKt {
public static final fun newRequestException (Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)Ldev/inmo/tgbotapi/bot/exceptions/RequestException;
public static synthetic fun newRequestException$default (Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)Ldev/inmo/tgbotapi/bot/exceptions/RequestException;
public static final fun newRequestException (Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)Ldev/inmo/tgbotapi/bot/exceptions/BotException;
public static synthetic fun newRequestException$default (Ldev/inmo/tgbotapi/types/Response;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)Ldev/inmo/tgbotapi/bot/exceptions/BotException;
}
public final class dev/inmo/tgbotapi/bot/exceptions/TooMuchRequestsException : dev/inmo/tgbotapi/bot/exceptions/RequestException {

View File

@@ -5,6 +5,7 @@ package dev.inmo.tgbotapi.bot.exceptions
import korlibs.time.DateTime
import dev.inmo.tgbotapi.types.Response
import dev.inmo.tgbotapi.types.RetryAfterError
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
import kotlinx.coroutines.CopyableThrowable
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.io.IOException
@@ -35,19 +36,51 @@ fun newRequestException(
message,
cause
)
response.errorCode != null -> ApiException(
response.errorCode,
plainAnswer,
response
)
else -> null
}
} ?: CommonRequestException(response, plainAnswer, message, cause)
sealed class BotException(message: String = "Something went wrong", cause: Throwable? = null) : IOException(message, cause), CopyableThrowable<BotException>
@ClassCastsIncluded
sealed class BotException(
override val message: String = "Something went wrong",
cause: Throwable? = null,
open val response: Response? = null,
open val plainAnswer: String? = null,
) : IOException(message, cause), CopyableThrowable<BotException>
class CommonBotException(message: String = "Something went wrong", cause: Throwable? = null) : BotException(message, cause) {
override fun createCopy(): BotException = CommonBotException(message!!, cause)
sealed class CommonBotException(
message: String = "Something went wrong",
cause: Throwable? = null,
response: Response? = null,
plainAnswer: String? = null,
) : BotException(message, cause, response, plainAnswer) {
class Default(message: String = "Something went wrong", cause: Throwable? = null) : CommonBotException(message, cause) {
override fun createCopy(): Default = Default(message, cause)
}
abstract override fun createCopy(): CommonBotException?
companion object {
operator fun invoke(message: String = "Something went wrong", cause: Throwable? = null) = Default(message, cause)
}
}
class ApiException(
val httpResponseCode: Int?,
val plainResponse: String,
response: Response? = null,
) :
CommonBotException("$httpResponseCode: $plainResponse", null, response, plainResponse) {
override fun createCopy(): ApiException = ApiException(httpResponseCode, plainResponse, response)
}
sealed class RequestException (
val response: Response,
val plainAnswer: String,
override val response: Response,
override val plainAnswer: String,
message: String? = null,
cause: Throwable? = null
) : BotException(

View File

@@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.kslog.common.*
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.exceptions.ApiException
import dev.inmo.tgbotapi.bot.exceptions.BotException
import dev.inmo.tgbotapi.bot.exceptions.CommonBotException
import dev.inmo.tgbotapi.bot.exceptions.GetUpdatesConflict
@@ -98,7 +99,15 @@ class DefaultKtorRequestsExecutor internal constructor(
)
}
exceptionResult.exceptionOrNull() ?.let {
CommonBotException(cause = e)
val prehandledException = runCatching {
when {
it is ResponseException -> ApiException(it.response.status.value, it.response.bodyAsText())
else -> null
}
}.getOrElse {
null
}
prehandledException ?: CommonBotException(cause = e)
} ?: exceptionResult.getOrThrow()
}
is CancellationException,