13
13
use Magento \Framework \DataObjectFactory ;
14
14
use Magento \Framework \Event \ManagerInterface as EventManagerInterface ;
15
15
use Sentry \Integration \IntegrationInterface ;
16
+ use Symfony \Component \Console \Command \Command ;
16
17
use Throwable ;
17
18
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
+ */
18
24
class GlobalExceptionCatcher
19
25
{
26
+ /** @var bool Whether the globalExceptionHandler is already attached */
27
+ private bool $ booted = false ;
28
+
20
29
/**
21
30
* GlobalExceptionCatcher constructor.
22
31
*
@@ -42,13 +51,56 @@ public function __construct(
42
51
*
43
52
* @param AppInterface $subject
44
53
* @param callable $proceed
54
+ * @param mixed $args
45
55
*
46
56
* @return \Magento\Framework\App\ResponseInterface
47
57
*/
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 )
49
95
{
96
+ if ($ this ->booted ) {
97
+ return $ proceed (...$ args );
98
+ }
99
+
100
+ $ this ->booted = true ;
101
+
50
102
if ((!$ this ->sentryHelper ->isActive ()) || (!$ this ->sentryHelper ->isPhpTrackingEnabled ())) {
51
- return $ proceed ();
103
+ return $ proceed (... $ args );
52
104
}
53
105
54
106
$ config = $ this ->prepareConfig ();
@@ -57,7 +109,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed)
57
109
$ this ->sentryPerformance ->startTransaction ($ subject );
58
110
59
111
try {
60
- return $ response = $ proceed ();
112
+ return $ response = $ proceed (... $ args );
61
113
} catch (Throwable $ exception ) {
62
114
$ this ->sentryInteraction ->captureException ($ exception );
63
115
0 commit comments