Skip to content

Commit 4371085

Browse files
authored
Fix command input handling (#589)
1 parent fd85d9b commit 4371085

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix extracting command input resulting in errors when calling Artisan commands programatically with `null` as an argument value (#589)
6+
57
## 2.14.1
68

79
- Fix not setting the correct SDK ID and version when running the `sentry:test` command (#582)

src/Sentry/Laravel/EventHandler.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Sentry\Breadcrumb;
2828
use Sentry\SentrySdk;
2929
use Sentry\State\Scope;
30+
use Symfony\Component\Console\Input\ArgvInput;
31+
use Symfony\Component\Console\Input\InputInterface;
3032

3133
class EventHandler
3234
{
@@ -568,9 +570,9 @@ protected function commandStartingHandler(CommandStarting $event)
568570
Breadcrumb::TYPE_DEFAULT,
569571
'artisan.command',
570572
'Starting Artisan command: ' . $event->command,
571-
method_exists($event->input, '__toString') ? [
572-
'input' => (string)$event->input,
573-
] : []
573+
[
574+
'input' => $this->extractConsoleCommandInput($event->input),
575+
]
574576
));
575577
}
576578
}
@@ -588,20 +590,35 @@ protected function commandFinishedHandler(CommandFinished $event)
588590
Breadcrumb::TYPE_DEFAULT,
589591
'artisan.command',
590592
'Finished Artisan command: ' . $event->command,
591-
array_merge([
593+
[
592594
'exit' => $event->exitCode,
593-
], method_exists($event->input, '__toString') ? [
594-
'input' => (string)$event->input,
595-
] : [])
595+
'input' => $this->extractConsoleCommandInput($event->input),
596+
]
596597
));
597598
}
598599

600+
// Flush any and all events that were possibly generated by the command
601+
Integration::flushEvents();
602+
599603
Integration::configureScope(static function (Scope $scope): void {
600-
$scope->setTag('command', '');
604+
$scope->removeTag('command');
601605
});
606+
}
602607

603-
// Flush any and all events that were possibly generated by the command
604-
Integration::flushEvents();
608+
/**
609+
* Extract the command input arguments if possible.
610+
*
611+
* @param \Symfony\Component\Console\Input\InputInterface|null $input
612+
*
613+
* @return string|null
614+
*/
615+
private function extractConsoleCommandInput(?InputInterface $input): ?string
616+
{
617+
if ($input instanceof ArgvInput) {
618+
return (string)$input;
619+
}
620+
621+
return null;
605622
}
606623

607624
protected function octaneRequestReceivedHandler(Octane\RequestReceived $event): void

test/Sentry/CommandInfoInBreadcrumbsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Sentry\Laravel\Tests;
44

55
use Illuminate\Console\Events\CommandStarting;
6-
use Symfony\Component\Console\Input\ArrayInput;
6+
use Symfony\Component\Console\Input\ArgvInput;
77
use Symfony\Component\Console\Output\BufferedOutput;
88

99
class CommandInfoInBreadcrumbsTest extends SentryLaravelTestCase
@@ -55,7 +55,7 @@ private function dispatchCommandStartEvent()
5555
CommandStarting::class,
5656
new CommandStarting(
5757
'test:command',
58-
new ArrayInput(['--foo' => 'bar']),
58+
new ArgvInput(['artisan', '--foo=bar']),
5959
new BufferedOutput()
6060
)
6161
);

0 commit comments

Comments
 (0)