Skip to content

Commit

Permalink
[fix] handle webhook exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Jan 26, 2024
1 parent adfb15d commit 7397e1d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
'failed_to_get_log_from_telegram' => 'Failed to get log from Telegram server',
'failed_to_register_webhook' => 'Failed to register webhook',
'failed_to_unregister_webhook' => 'Failed to unregister webhook',
'webhook_error_occurred' => 'Sorry, an error occurred',
];
74 changes: 45 additions & 29 deletions src/Handlers/WebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Illuminate\Support\Stringable;
use ReflectionMethod;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;

abstract class WebhookHandler
{
Expand Down Expand Up @@ -65,7 +66,7 @@ private function handleCallbackQuery(): void
return;
}

/** @phpstan-ignore-next-line */
/** @phpstan-ignore-next-line */
App::call([$this, $action], $this->data->toArray());
}

Expand Down Expand Up @@ -220,44 +221,48 @@ public function chatid(): void

public function handle(Request $request, TelegraphBot $bot): void
{
$this->bot = $bot;
try {
$this->bot = $bot;

$this->request = $request;
$this->request = $request;

if ($this->request->has('message')) {
/* @phpstan-ignore-next-line */
$this->message = Message::fromArray($this->request->input('message'));
$this->handleMessage();
if ($this->request->has('message')) {
/* @phpstan-ignore-next-line */
$this->message = Message::fromArray($this->request->input('message'));
$this->handleMessage();

return;
}
return;
}

if ($this->request->has('edited_message')) {
/* @phpstan-ignore-next-line */
$this->message = Message::fromArray($this->request->input('edited_message'));
$this->handleMessage();
if ($this->request->has('edited_message')) {
/* @phpstan-ignore-next-line */
$this->message = Message::fromArray($this->request->input('edited_message'));
$this->handleMessage();

return;
}
return;
}

if ($this->request->has('channel_post')) {
/* @phpstan-ignore-next-line */
$this->message = Message::fromArray($this->request->input('channel_post'));
$this->handleMessage();
if ($this->request->has('channel_post')) {
/* @phpstan-ignore-next-line */
$this->message = Message::fromArray($this->request->input('channel_post'));
$this->handleMessage();

return;
}
return;
}


if ($this->request->has('callback_query')) {
/* @phpstan-ignore-next-line */
$this->callbackQuery = CallbackQuery::fromArray($this->request->input('callback_query'));
$this->handleCallbackQuery();
}
if ($this->request->has('callback_query')) {
/* @phpstan-ignore-next-line */
$this->callbackQuery = CallbackQuery::fromArray($this->request->input('callback_query'));
$this->handleCallbackQuery();
}

if ($this->request->has('inline_query')) {
/* @phpstan-ignore-next-line */
$this->handleInlineQuery(InlineQuery::fromArray($this->request->input('inline_query')));
if ($this->request->has('inline_query')) {
/* @phpstan-ignore-next-line */
$this->handleInlineQuery(InlineQuery::fromArray($this->request->input('inline_query')));
}
} catch (Throwable $throwable) {
$this->onFailure($throwable);
}
}

Expand Down Expand Up @@ -300,4 +305,15 @@ protected function allowUnknownChat(): bool
default => false,
};
}

protected function onFailure(Throwable $throwable): void
{
if ($throwable instanceof NotFoundHttpException) {
throw $throwable;
}

report($throwable);

$this->reply(__('telegraph::errors.webhook_error_occurred'));
}
}
6 changes: 6 additions & 0 deletions tests/Support/TestWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DefStudio\Telegraph\Handlers\WebhookHandler;
use DefStudio\Telegraph\Keyboard\Button;
use DefStudio\Telegraph\Keyboard\Keyboard;
use Exception;
use Illuminate\Support\Stringable;

class TestWebhookHandler extends WebhookHandler
Expand All @@ -32,6 +33,11 @@ public function test(): void
self::$calls_count++;
}

public function trigger_failure()
{
throw new Exception('foo');
}

public function send_reply(): void
{
$this->reply('foo');
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Handlers/WebhookHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,16 @@

Facade::assertSent("Bob just left");
});

it('does not crash on errors', function () {

$chat = chat();

Facade::fake();

app(TestWebhookHandler::class)
->handle(webhook_request('trigger_failure'), $chat->bot)
;

Facade::assertRepliedWebhook('Sorry, an error occurred');
});

0 comments on commit 7397e1d

Please sign in to comment.