Handle exceptions in command and event handlers

This commit is contained in:
netkas 2024-11-05 15:42:34 -05:00
parent 12a091ce80
commit 2900b708da
3 changed files with 52 additions and 17 deletions

View file

@ -4,6 +4,7 @@
use TgBotLib\Bot; use TgBotLib\Bot;
use TgBotLib\Enums\EventType; use TgBotLib\Enums\EventType;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Objects\Update; use TgBotLib\Objects\Update;
abstract class UpdateEvent abstract class UpdateEvent
@ -35,6 +36,7 @@
* *
* @param Bot $bot The bot instance to be handled. * @param Bot $bot The bot instance to be handled.
* @return void * @return void
* @throws TelegramException
*/ */
public abstract function handle(Bot $bot): void; public abstract function handle(Bot $bot): void;
} }

View file

@ -9,6 +9,7 @@
namespace TgBotLib; namespace TgBotLib;
use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use ReflectionClass; use ReflectionClass;
use TgBotLib\Abstracts\Method; use TgBotLib\Abstracts\Method;
@ -366,14 +367,25 @@
if($command !== null) if($command !== null)
{ {
$commandExecuted = false; $commandExecuted = false;
Logger::getLogger()->debug(sprintf('Executing command %s for update %s', $command, $update->getUpdateId()));
/** @var CommandEvent $eventHandler */ /** @var CommandEvent $eventHandler */
foreach($this->getEventHandlersByCommand($command) as $eventHandler) foreach($this->getEventHandlersByCommand($command) as $eventHandler)
{ {
Logger::getLogger()->debug(sprintf('Executing command %s for update %s', $command, $update->getUpdateId())); try
{
(new $eventHandler($update))->handle($this); (new $eventHandler($update))->handle($this);
$commandExecuted = true; $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) if($commandExecuted)
{ {
@ -387,12 +399,23 @@
// If there are no appropriate event handlers for the update type, use the generic update event handler // If there are no appropriate event handlers for the update type, use the generic update event handler
if(empty($eventHandlers)) 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) foreach ($this->getEventHandlersByType(EventType::UPDATE_EVENT) as $eventHandler)
{ {
Logger::getLogger()->debug(sprintf('Executing generic update event handler for update %s', $update->getUpdateId())); try
{
/** @var UpdateEvent $eventHandler */ /** @var UpdateEvent $eventHandler */
(new $eventHandler($update))->handle($this); (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 // We return early here to avoid executing the generic update event handler twice
return; return;
@ -400,11 +423,22 @@
// Execute all event handlers that match the update type // Execute all event handlers that match the update type
/** @var UpdateEvent $eventHandler */ /** @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) foreach($eventHandlers as $eventHandler)
{ {
Logger::getLogger()->debug(sprintf('Executing event handler for type %s for update %s', $eventHandler::getEventType()->value, $update->getUpdateId())); try
{
(new $eventHandler($update))->handle($this); (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 // Support named and positional arguments
Logger::getLogger()->debug(sprintf('Calling method %s with arguments %s', $name, json_encode($arguments)));
$parameters = $this->parseArguments($name, $arguments); $parameters = $this->parseArguments($name, $arguments);
return $this->sendRequest($name, $parameters); return $this->sendRequest($name, $parameters);
} }

View file

@ -353,9 +353,9 @@
return $this->text; 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; return $this->text;
@ -514,7 +514,7 @@
if(preg_match('/^\/([a-zA-Z0-9_]+)(?:@[a-zA-Z0-9_]+)?/', $this->caption, $matches)) 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; return $this->caption;
@ -1041,12 +1041,12 @@
*/ */
public function getCommand(): ?string public function getCommand(): ?string
{ {
if ($this->getAnyText() === null) if ($this->getAnyText(true) === null)
{ {
return null; return null;
} }
$text = trim($this->getAnyText()); $text = trim($this->getAnyText(true));
if (!str_starts_with($text, '/')) if (!str_starts_with($text, '/'))
{ {