diff --git a/CHANGELOG.md b/CHANGELOG.md index 3716e7345c..973cf0fa86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * `SetWebhook` factories signatures was changed (backward compatibility was not broken) * `executeUnsafe` now working differently + * Now it is possible to pass exceptions handler into `executeUnsafe` * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt index 0dc1f6f868..6e4a4f50ef 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt @@ -34,19 +34,23 @@ fun RequestsExecutor.executeAsync( suspend fun RequestsExecutor.executeUnsafe( request: Request, retries: Int = 0, - retriesDelay: Long = 1000L + retriesDelay: Long = 1000L, + onAllFailed: (suspend (exceptions: Array) -> Unit)? = null ): T? { var leftRetries = retries + val exceptions = onAllFailed ?.let { mutableListOf() } do { handleSafely( { leftRetries-- delay(retriesDelay) + exceptions ?.add(it) null } ) { execute(request) } ?.let { return it } } while(leftRetries >= 0) + onAllFailed ?.invoke(exceptions ?.toTypedArray() ?: emptyArray()) return null }