vendor/sentry/sentry-symfony/src/EventListener/ConsoleListener.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  4. use Sentry\State\HubInterface;
  5. use Sentry\State\Scope;
  6. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  7. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  8. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  9. use Symfony\Component\Console\Input\ArgvInput;
  10. /**
  11.  * This listener handles all errors thrown while running a console command and
  12.  * logs them to Sentry.
  13.  *
  14.  * @final since version 4.1
  15.  */
  16. class ConsoleListener
  17. {
  18.     /**
  19.      * @var HubInterface The current hub
  20.      */
  21.     private $hub;
  22.     /**
  23.      * @var bool Whether to capture console errors
  24.      */
  25.     private $captureErrors;
  26.     /**
  27.      * Constructor.
  28.      *
  29.      * @param HubInterface $hub           The current hub
  30.      * @param bool         $captureErrors Whether to capture console errors
  31.      */
  32.     public function __construct(HubInterface $hubbool $captureErrors true)
  33.     {
  34.         $this->hub $hub;
  35.         $this->captureErrors $captureErrors;
  36.     }
  37.     /**
  38.      * Handles the execution of a console command by pushing a new {@see Scope}.
  39.      *
  40.      * @param ConsoleCommandEvent $event The event
  41.      */
  42.     public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
  43.     {
  44.         $scope $this->hub->pushScope();
  45.         $command $event->getCommand();
  46.         $input $event->getInput();
  47.         if (null !== $command && null !== $command->getName()) {
  48.             $scope->setTag('console.command'$command->getName());
  49.         }
  50.         if ($input instanceof ArgvInput) {
  51.             $scope->setExtra('Full command', (string) $input);
  52.         }
  53.     }
  54.     /**
  55.      * Handles the termination of a console command by popping the {@see Scope}.
  56.      *
  57.      * @param ConsoleTerminateEvent $event The event
  58.      */
  59.     public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
  60.     {
  61.         $this->hub->popScope();
  62.     }
  63.     /**
  64.      * Handles an error that happened while running a console command.
  65.      *
  66.      * @param ConsoleErrorEvent $event The event
  67.      */
  68.     public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
  69.     {
  70.         $this->hub->configureScope(function (Scope $scope) use ($event): void {
  71.             $scope->setTag('console.command.exit_code', (string) $event->getExitCode());
  72.             if ($this->captureErrors) {
  73.                 $this->hub->captureException($event->getError());
  74.             }
  75.         });
  76.     }
  77. }