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\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;
}

View file

@ -9,6 +9,7 @@
namespace TgBotLib;
use Exception;
use InvalidArgumentException;
use ReflectionClass;
use TgBotLib\Abstracts\Method;
@ -366,14 +367,25 @@
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()));
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,12 +399,23 @@
// 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()));
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
return;
@ -400,11 +423,22 @@
// 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()));
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);
}

View file

@ -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, '/'))
{