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

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