Skip to content

Commit 2b51fa5

Browse files
stayalliveHazAT
authored andcommitted
Add missing event handler (#180)
* Placeholder for missing event handler * Guard against missing event handlers causing infinite loop * fix: push/pop Scope on job processing
1 parent fb6ed71 commit 2b51fa5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Sentry/Laravel/EventHandler.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use Illuminate\Console\Events\CommandStarting;
1414
use Illuminate\Console\Events\CommandFinished;
1515
use Illuminate\Routing\Route;
16+
use RuntimeException;
1617
use Sentry\State\Scope;
1718
use Sentry\Breadcrumb;
19+
use Sentry\State\Hub;
1820

1921
class EventHandler
2022
{
@@ -98,6 +100,10 @@ public function subscribeAuthEvents(Dispatcher $events)
98100
*/
99101
public function __call($method, $arguments)
100102
{
103+
if (!method_exists($this, $method . 'handler')) {
104+
throw new RuntimeException('Missing event handler:' . $method . 'handler');
105+
}
106+
101107
try {
102108
call_user_func_array(array($this, $method . 'handler'), $arguments);
103109
} catch (Exception $exception) {
@@ -132,6 +138,7 @@ protected function routerMatchedHandler(Route $route)
132138
));
133139
Integration::setTransaction($routeName);
134140
}
141+
135142
/**
136143
* Since Laravel 5.2
137144
*
@@ -142,7 +149,6 @@ protected function routeMatchedHandler(RouteMatched $match)
142149
$this->routerMatchedHandler($match->route);
143150
}
144151

145-
146152
/**
147153
* Since Laravel 5.2
148154
*
@@ -202,6 +208,9 @@ protected function authenticatedHandler(Authenticated $event)
202208
*/
203209
protected function queueJobProcessingHandler(JobProcessing $event)
204210
{
211+
// When a job starts, we want to push a new scope
212+
Hub::getCurrent()->pushScope();
213+
205214
$job = [
206215
'job' => $event->job->getName(),
207216
'queue' => $event->job->getQueue(),
@@ -223,6 +232,18 @@ protected function queueJobProcessingHandler(JobProcessing $event)
223232
));
224233
}
225234

235+
236+
/**
237+
* Since Laravel 5.2
238+
*
239+
* @param \Illuminate\Queue\Events\JobProcessed $event
240+
*/
241+
protected function queueJobProcessedHandler(JobProcessed $event)
242+
{
243+
// When a job finished, we want to pop the scope
244+
Hub::getCurrent()->popScope();
245+
}
246+
226247
/**
227248
* Since Laravel 5.5
228249
*

test/Sentry/EventHandlerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class EventHandlerTest extends \PHPUnit\Framework\TestCase
4+
{
5+
/**
6+
* @expectedException \RuntimeException
7+
*/
8+
public function test_missing_event_handler_throws_exception()
9+
{
10+
$handler = new \Sentry\Laravel\EventHandler([]);
11+
12+
$handler->thisIsNotAHandlerAndShouldThrowAnException();
13+
}
14+
}

0 commit comments

Comments
 (0)