Skip to content

Commit 3e4a4ed

Browse files
authored
Catch errors thrown from commands (#186)
1 parent 80aae37 commit 3e4a4ed

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Model/SentryPerformance.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Sentry\Tracing\Transaction;
2222
use Sentry\Tracing\TransactionContext;
2323
use Sentry\Tracing\TransactionSource;
24+
use Symfony\Component\Console\Command\Command;
2425
use Throwable;
2526

2627
use function Sentry\startTransaction;
@@ -49,11 +50,11 @@ public function __construct(
4950
/**
5051
* Starts a new transaction.
5152
*
52-
* @param AppInterface $app
53+
* @param Command|AppInterface $app
5354
*
5455
* @return void
5556
*/
56-
public function startTransaction(AppInterface $app): void
57+
public function startTransaction(Command|AppInterface $app): void
5758
{
5859
if (!$app instanceof Http) {
5960
// We only support profiling of http requests right now.

Plugin/GlobalExceptionCatcher.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
use Magento\Framework\DataObjectFactory;
1414
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
1515
use Sentry\Integration\IntegrationInterface;
16+
use Symfony\Component\Console\Command\Command;
1617
use Throwable;
1718

19+
/**
20+
* Catch all uncaught exceptions globally, and send them to Sentry.
21+
*
22+
* It wraps the launch and run methods of the application and command classes.
23+
*/
1824
class GlobalExceptionCatcher
1925
{
26+
/** @var bool Whether the globalExceptionHandler is already attached */
27+
private bool $booted = false;
28+
2029
/**
2130
* GlobalExceptionCatcher constructor.
2231
*
@@ -42,13 +51,56 @@ public function __construct(
4251
*
4352
* @param AppInterface $subject
4453
* @param callable $proceed
54+
* @param mixed $args
4555
*
4656
* @return \Magento\Framework\App\ResponseInterface
4757
*/
48-
public function aroundLaunch(AppInterface $subject, callable $proceed)
58+
public function aroundLaunch(AppInterface $subject, callable $proceed, ...$args)
59+
{
60+
return $this->globalCatcher(
61+
$subject,
62+
$proceed,
63+
...$args
64+
);
65+
}
66+
67+
/**
68+
* Wrap command run, start watching for exceptions.
69+
*
70+
* @param Command $subject
71+
* @param callable $proceed
72+
* @param mixed $args
73+
*
74+
* @return int
75+
*/
76+
public function aroundRun(Command $subject, callable $proceed, ...$args)
77+
{
78+
return $this->globalCatcher(
79+
$subject,
80+
$proceed,
81+
...$args
82+
);
83+
}
84+
85+
/**
86+
* Catch anything coming out of the proceed function.
87+
*
88+
* @param mixed $subject
89+
* @param callable $proceed
90+
* @param mixed $args
91+
*
92+
* @return mixed
93+
*/
94+
public function globalCatcher($subject, $proceed, ...$args)
4995
{
96+
if ($this->booted) {
97+
return $proceed(...$args);
98+
}
99+
100+
$this->booted = true;
101+
50102
if ((!$this->sentryHelper->isActive()) || (!$this->sentryHelper->isPhpTrackingEnabled())) {
51-
return $proceed();
103+
return $proceed(...$args);
52104
}
53105

54106
$config = $this->prepareConfig();
@@ -57,7 +109,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed)
57109
$this->sentryPerformance->startTransaction($subject);
58110

59111
try {
60-
return $response = $proceed();
112+
return $response = $proceed(...$args);
61113
} catch (Throwable $exception) {
62114
$this->sentryInteraction->captureException($exception);
63115

etc/di.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<plugin disabled="false" name="GlobalExceptionCatcher" type="JustBetter\Sentry\Plugin\GlobalExceptionCatcher"
88
sortOrder="0"/>
99
</type>
10+
<type name="Symfony\Component\Console\Command\Command">
11+
<plugin disabled="false" name="CliExceptionCatcher" type="JustBetter\Sentry\Plugin\GlobalExceptionCatcher"
12+
sortOrder="0"/>
13+
</type>
1014

1115
<type name="Magento\Customer\CustomerData\Customer">
1216
<plugin name="LogrocketCustomerInfo" type="JustBetter\Sentry\Plugin\LogrocketCustomerInfo"
@@ -20,7 +24,7 @@
2024
</argument>
2125
</arguments>
2226
</type>
23-
27+
2428
<type name="JustBetter\Sentry\Model\SentryLog">
2529
<arguments>
2630
<argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</argument>

0 commit comments

Comments
 (0)