From 2900b708da26e001acfde5ecda0bdfa4934b1b39 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 5 Nov 2024 15:42:34 -0500 Subject: [PATCH] Handle exceptions in command and event handlers --- src/TgBotLib/Abstracts/UpdateEvent.php | 2 + src/TgBotLib/Bot.php | 51 +++++++++++++++++++++----- src/TgBotLib/Objects/Message.php | 16 ++++---- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/TgBotLib/Abstracts/UpdateEvent.php b/src/TgBotLib/Abstracts/UpdateEvent.php index 69df341..83e426a 100644 --- a/src/TgBotLib/Abstracts/UpdateEvent.php +++ b/src/TgBotLib/Abstracts/UpdateEvent.php @@ -4,6 +4,7 @@ use TgBotLib\Bot; use TgBotLib\Enums\EventType; + use TgBotLib\Exceptions\TelegramException; use TgBotLib\Objects\Update; abstract class UpdateEvent @@ -35,6 +36,7 @@ * * @param Bot $bot The bot instance to be handled. * @return void + * @throws TelegramException */ public abstract function handle(Bot $bot): void; } \ No newline at end of file diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 1b21a77..a558f96 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -9,6 +9,7 @@ namespace TgBotLib; + use Exception; use InvalidArgumentException; use ReflectionClass; use TgBotLib\Abstracts\Method; @@ -366,13 +367,24 @@ if($command !== null) { $commandExecuted = false; + Logger::getLogger()->debug(sprintf('Executing command %s for update %s', $command, $update->getUpdateId())); /** @var CommandEvent $eventHandler */ foreach($this->getEventHandlersByCommand($command) as $eventHandler) { - Logger::getLogger()->debug(sprintf('Executing command %s for update %s', $command, $update->getUpdateId())); - (new $eventHandler($update))->handle($this); - $commandExecuted = true; + try + { + (new $eventHandler($update))->handle($this); + $commandExecuted = true; + } + catch(TelegramException $e) + { + Logger::getLogger()->error(sprintf('Telegram exception occurred: %s', $e->getMessage()), $e); + } + catch(Exception $e) + { + Logger::getLogger()->error(sprintf('Exception occurred: %s', $e->getMessage()), $e); + } } if($commandExecuted) @@ -387,11 +399,22 @@ // If there are no appropriate event handlers for the update type, use the generic update event handler if(empty($eventHandlers)) { + Logger::getLogger()->debug(sprintf('Executing generic update event handler for update %s', $update->getUpdateId())); foreach ($this->getEventHandlersByType(EventType::UPDATE_EVENT) as $eventHandler) { - Logger::getLogger()->debug(sprintf('Executing generic update event handler for update %s', $update->getUpdateId())); - /** @var UpdateEvent $eventHandler */ - (new $eventHandler($update))->handle($this); + try + { + /** @var UpdateEvent $eventHandler */ + (new $eventHandler($update))->handle($this); + } + catch(TelegramException $e) + { + Logger::getLogger()->error(sprintf('Telegram exception occurred: %s', $e->getMessage()), $e); + } + catch(Exception $e) + { + Logger::getLogger()->error(sprintf('Exception occurred: %s', $e->getMessage()), $e); + } } // We return early here to avoid executing the generic update event handler twice @@ -400,10 +423,21 @@ // Execute all event handlers that match the update type /** @var UpdateEvent $eventHandler */ + Logger::getLogger()->debug(sprintf('Executing event handler for type %s for update %s', $eventHandler::getEventType()->value, $update->getUpdateId())); foreach($eventHandlers as $eventHandler) { - Logger::getLogger()->debug(sprintf('Executing event handler for type %s for update %s', $eventHandler::getEventType()->value, $update->getUpdateId())); - (new $eventHandler($update))->handle($this); + try + { + (new $eventHandler($update))->handle($this); + } + catch(TelegramException $e) + { + Logger::getLogger()->error(sprintf('Telegram exception occurred: %s', $e->getMessage()), $e); + } + catch(Exception $e) + { + Logger::getLogger()->error(sprintf('Exception occurred: %s', $e->getMessage()), $e); + } } } @@ -467,7 +501,6 @@ } // Support named and positional arguments - Logger::getLogger()->debug(sprintf('Calling method %s with arguments %s', $name, json_encode($arguments))); $parameters = $this->parseArguments($name, $arguments); return $this->sendRequest($name, $parameters); } diff --git a/src/TgBotLib/Objects/Message.php b/src/TgBotLib/Objects/Message.php index 7749224..eea2524 100644 --- a/src/TgBotLib/Objects/Message.php +++ b/src/TgBotLib/Objects/Message.php @@ -341,21 +341,21 @@ * @param bool $includeCommand * @return string|null */ - public function getText(bool $includeCommand=false): ?string + public function getText(bool $includeCommand = false): ?string { - if($this->text === null) + if ($this->text === null) { return null; } - if($includeCommand) + if ($includeCommand) { return $this->text; } - if(preg_match('/^\/([a-zA-Z0-9_]+)(?:@[a-zA-Z0-9_]+)?/', $this->text, $matches)) + if (preg_match('/^\/([a-zA-Z0-9_]+)(?:@[a-zA-Z0-9_]+)?\s?/', $this->text, $matches)) { - return str_replace($matches[0], '', $this->text); + return ltrim(substr($this->text, strlen($matches[0]))); } return $this->text; @@ -514,7 +514,7 @@ if(preg_match('/^\/([a-zA-Z0-9_]+)(?:@[a-zA-Z0-9_]+)?/', $this->caption, $matches)) { - return str_replace($matches[0], '', $this->caption); + return ltrim(substr($this->caption, strlen($matches[0]))); } return $this->caption; @@ -1041,12 +1041,12 @@ */ public function getCommand(): ?string { - if ($this->getAnyText() === null) + if ($this->getAnyText(true) === null) { return null; } - $text = trim($this->getAnyText()); + $text = trim($this->getAnyText(true)); if (!str_starts_with($text, '/')) {