From 9a0d2c28d93c7a5af5a0643cb53e9b507713738d Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Wed, 12 Oct 2022 09:56:54 +0200 Subject: [PATCH] Fix command input handling --- src/Sentry/Laravel/EventHandler.php | 26 ++++++++++++++------ test/Sentry/CommandInfoInBreadcrumbsTest.php | 4 +-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index f9f32bfe..39fccea7 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -27,6 +27,9 @@ use Sentry\Breadcrumb; use Sentry\SentrySdk; use Sentry\State\Scope; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputInterface; class EventHandler { @@ -568,9 +571,9 @@ protected function commandStartingHandler(CommandStarting $event) Breadcrumb::TYPE_DEFAULT, 'artisan.command', 'Starting Artisan command: ' . $event->command, - method_exists($event->input, '__toString') ? [ - 'input' => (string)$event->input, - ] : [] + [ + 'input' => $this->extractCommandInput($event->input), + ] )); } } @@ -588,11 +591,10 @@ protected function commandFinishedHandler(CommandFinished $event) Breadcrumb::TYPE_DEFAULT, 'artisan.command', 'Finished Artisan command: ' . $event->command, - array_merge([ + [ 'exit' => $event->exitCode, - ], method_exists($event->input, '__toString') ? [ - 'input' => (string)$event->input, - ] : []) + 'input' => $this->extractCommandInput($event->input), + ] )); } @@ -604,6 +606,16 @@ protected function commandFinishedHandler(CommandFinished $event) Integration::flushEvents(); } + /** @return string|null */ + private function extractCommandInput(InputInterface $input) + { + if ($input instanceof ArgvInput) { + return (string)$input; + } + + return null; + } + protected function octaneRequestReceivedHandler(Octane\RequestReceived $event): void { $this->prepareScopeForOctane(); diff --git a/test/Sentry/CommandInfoInBreadcrumbsTest.php b/test/Sentry/CommandInfoInBreadcrumbsTest.php index f0b0a61c..7947656f 100644 --- a/test/Sentry/CommandInfoInBreadcrumbsTest.php +++ b/test/Sentry/CommandInfoInBreadcrumbsTest.php @@ -3,7 +3,7 @@ namespace Sentry\Laravel\Tests; use Illuminate\Console\Events\CommandStarting; -use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\BufferedOutput; class CommandInfoInBreadcrumbsTest extends SentryLaravelTestCase @@ -55,7 +55,7 @@ private function dispatchCommandStartEvent() CommandStarting::class, new CommandStarting( 'test:command', - new ArrayInput(['--foo' => 'bar']), + new ArgvInput(['artisan', '--foo=bar']), new BufferedOutput() ) );