Skip to content

Commit

Permalink
log exceptions between update handles instead of throwing them
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger committed Sep 1, 2024
1 parent fca1880 commit d6d45ee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Response tags and subscriptions are added.
- Params section renamed from `botasis/telegram-bot` to `botasis/runtime`.
- `yiisoft/config`-compatible configuration added for all possible classes. Dynamic part is moved to params.
- `GetUpdatesCommand` don't throw exceptions anymore. It just logs them to a PSR logger instead.
- More docs are added

## 0.11.3
Expand All @@ -35,7 +36,6 @@
- Fix failed tests
- PhpUnit cache is removed from the repository
- Telegram Update variables are completely renamed from $request to $update
- Telegram Update variables are completely renamed from $request to $update

## 0.11.0

Expand Down
13 changes: 11 additions & 2 deletions src/Console/GetUpdatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Botasis\Client\Telegram\Request\TelegramRequest;
use Botasis\Runtime\Application;
use Botasis\Runtime\Update\UpdateFactory;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

final class GetUpdatesCommand extends Command
{
Expand All @@ -22,6 +25,7 @@ public function __construct(
private readonly ClientInterface $client,
private readonly Application $application,
private readonly UpdateFactory $updateFactory,
private readonly LoggerInterface $logger = new NullLogger(),
string $name = null,
) {
parent::__construct($name);
Expand All @@ -45,8 +49,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$request = new TelegramRequest('getUpdates', $data);

foreach ($this->client->send($request)['result'] ?? [] as $update) {
$update = $this->updateFactory->create($update);
$this->application->handle($update);
try {
$update = $this->updateFactory->create($update);
$this->application->handle($update);
} catch (Throwable $exception) {
// TODO Add an error handler? I.e. to send messages to Sentry.
$this->logger->error($exception);
}
}

if ($update !== null) {
Expand Down
1 change: 0 additions & 1 deletion src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public function match(Update $update): UpdateHandlerInterface
{
['key' => $key, 'route' => $route] = $this->routesStatic[$update->requestData] ?? ['key' => null, 'route' => null];
if ($route !== null) {
/** @psalm-suppress UndefinedPropertyFetch The rule property is always RuleStatic here */
if (!isset($this->compiledStatic[$route->rule->message])) {
$this->compiledStatic[$route->rule->message] = $this->compileRoute($route, (string) $key);
}
Expand Down

0 comments on commit d6d45ee

Please sign in to comment.