From 518dee46b3504cfeb4e67a66a6dfc1e5faad7310 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 19 Mar 2020 12:03:40 +0600 Subject: [PATCH] now startGettingOfUpdates have exceptions handler as argument --- .../extensions/api/updates/UpdatesPolling.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt index 6624211c2b..bd4e99f8ab 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.* fun RequestsExecutor.startGettingOfUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: (suspend (Exception) -> Unit)? = null, allowedUpdates: List? = null, updatesReceiver: UpdateReceiver ): Job = scope.launch { @@ -40,10 +41,12 @@ fun RequestsExecutor.startGettingOfUpdates( } } } catch (e: HttpRequestTimeoutException) { - e // it is ok due to mechanism of long polling + exceptionsHandler ?.invoke(e) // it is ok due to mechanism of long polling } catch (e: RequestException) { - e // it is not ok, but in most cases it will mean that there is some limit for requests count + exceptionsHandler ?.invoke(e) // it is not ok, but in most cases it will mean that there is some limit for requests count delay(1000L) + } catch (e: Exception) { + exceptionsHandler ?.invoke(e) } } } @@ -51,10 +54,12 @@ fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates( updatesFilter: UpdatesFilter, timeoutSeconds: Seconds = 30, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job = startGettingOfUpdates( timeoutSeconds, scope, + exceptionsHandler, updatesFilter.allowedUpdates, updatesFilter.asUpdateReceiver ) @@ -76,6 +81,7 @@ fun RequestsExecutor.startGettingOfUpdates( pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, timeoutSeconds: Seconds = 30, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, scope: CoroutineScope = GlobalScope ): Job { return startGettingOfUpdates( @@ -97,6 +103,7 @@ fun RequestsExecutor.startGettingOfUpdates( pollAnswerCallback ), timeoutSeconds, + exceptionsHandler, scope ) } @@ -115,6 +122,7 @@ fun RequestsExecutor.startGettingOfUpdates( pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, timeoutSeconds: Seconds = 30, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job = startGettingOfUpdates( messageCallback = messageCallback, @@ -133,5 +141,6 @@ fun RequestsExecutor.startGettingOfUpdates( pollCallback = pollCallback, pollAnswerCallback = pollAnswerCallback, timeoutSeconds = timeoutSeconds, + exceptionsHandler = exceptionsHandler, scope = scope )