From 4371085dd2e426e99a00b03b7242b862e58f28e6 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 13 Oct 2022 11:15:05 +0200 Subject: [PATCH] Fix command input handling (#589) --- CHANGELOG.md | 2 ++ src/Sentry/Laravel/EventHandler.php | 37 ++++++++++++++------ test/Sentry/CommandInfoInBreadcrumbsTest.php | 4 +-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c527e781..784de1e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Fix extracting command input resulting in errors when calling Artisan commands programatically with `null` as an argument value (#589) + ## 2.14.1 - Fix not setting the correct SDK ID and version when running the `sentry:test` command (#582) diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index f9f32bfe..5ad15db6 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -27,6 +27,8 @@ use Sentry\Breadcrumb; use Sentry\SentrySdk; use Sentry\State\Scope; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Input\InputInterface; class EventHandler { @@ -568,9 +570,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->extractConsoleCommandInput($event->input), + ] )); } } @@ -588,20 +590,35 @@ 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->extractConsoleCommandInput($event->input), + ] )); } + // Flush any and all events that were possibly generated by the command + Integration::flushEvents(); + Integration::configureScope(static function (Scope $scope): void { - $scope->setTag('command', ''); + $scope->removeTag('command'); }); + } - // Flush any and all events that were possibly generated by the command - Integration::flushEvents(); + /** + * Extract the command input arguments if possible. + * + * @param \Symfony\Component\Console\Input\InputInterface|null $input + * + * @return string|null + */ + private function extractConsoleCommandInput(?InputInterface $input): ?string + { + if ($input instanceof ArgvInput) { + return (string)$input; + } + + return null; } protected function octaneRequestReceivedHandler(Octane\RequestReceived $event): void 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() ) );