From fbc472d8de4558bcb56d11c68af70a37504a06c6 Mon Sep 17 00:00:00 2001 From: Netkas Date: Wed, 9 Aug 2023 20:02:59 -0400 Subject: [PATCH] Updated method `\TgBotLib > Bot > handleGetUpdates()` to handle exceptions --- .idea/php.xml | 6 +++--- CHANGELOG.md | 1 + src/TgBotLib/Bot.php | 16 ++++++++++++++-- tests/commands/HashCommand.php | 4 ++-- tests/commands/StartCommand.php | 2 +- tests/multi_threaded.php | 19 ------------------- 6 files changed, 21 insertions(+), 27 deletions(-) delete mode 100644 tests/multi_threaded.php diff --git a/.idea/php.xml b/.idea/php.xml index 14e3c93..91373c3 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -14,12 +14,12 @@ - - + + - + diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ea6984..4bda5d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ input objects for methods that require input objects. * Updated class type to `final class` in `\TgBotLib\Enums > StickerType` * Updated class type to `final class` in `\TgBotLib\Enums > ThumbnailMimeType` * Updated class type to `final class` in `\TgBotLib\Enums > UpdateEventType` + * Updated method `\TgBotLib > Bot > handleGetUpdates()` to handle exceptions ## [6.6.0] - 2023-04-10 diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 56b34e3..d5b7b45 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -505,14 +505,26 @@ * @return void * @throws TelegramException */ - public function handleGetUpdates(bool $run_forever = false): void + public function handleGetUpdates(bool $run_forever=false): void { do { $updates = $this->getUpdates(); foreach($updates as $update) { - $this->handleUpdate($update); + try + { + $this->handleUpdate($update); + } + catch(Exception $e) + { + Log::error('net.nosial.tgbotlib', sprintf('Unhandled exception while handling update: %s', $e->getMessage()), $e); + + if(!$run_forever) + { + throw new TelegramException('Unhandled exception while handling update', 0, $e); + } + } } } while($run_forever); } diff --git a/tests/commands/HashCommand.php b/tests/commands/HashCommand.php index 1a7d01f..bc5c692 100644 --- a/tests/commands/HashCommand.php +++ b/tests/commands/HashCommand.php @@ -18,9 +18,9 @@ public function handle(Bot $bot, Update $update): void { // Usage: /hash - $data = str_replace('/hash ', '', $update->getMessage()->getText()); + $data = str_replace('/hash ', '', $update->getMessage()?->getText()); $bot->sendMessage( - $update->getMessage()->getChat()->getId(), md5($data) + $update->getMessage()?->getChat()?->getId(), md5($data) ); } } \ No newline at end of file diff --git a/tests/commands/StartCommand.php b/tests/commands/StartCommand.php index e3ce303..8fb3d7e 100644 --- a/tests/commands/StartCommand.php +++ b/tests/commands/StartCommand.php @@ -19,7 +19,7 @@ { // reply to the incoming message $bot->sendMessage( - $update->getMessage()->getChat()->getId(), 'Hello, ' . $update->getMessage()->getFrom()->getFirstName() + $update->getMessage()?->getChat()?->getId(), 'Hello, ' . $update->getMessage()?->getFrom()?->getFirstName() ); } } \ No newline at end of file diff --git a/tests/multi_threaded.php b/tests/multi_threaded.php deleted file mode 100644 index 0338e9b..0000000 --- a/tests/multi_threaded.php +++ /dev/null @@ -1,19 +0,0 @@ -getUpdates() as $update) - { - TamerLib\Tamer::do(TamerLib\Objects\Task::create('handle_update', json_encode($update->toArray()))); - } - } \ No newline at end of file