Skip to content

Commit 0263ae6

Browse files
authored
Merge pull request #167 from ruudk/patch-2
ExceptionListener > Add `route` tag
2 parents c0154ea + 15b70d4 commit 0263ae6

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/EventListener/ExceptionListener.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,14 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
111111
return;
112112
}
113113

114+
$data = ['tags' => []];
115+
$request = $this->requestStack->getCurrentRequest();
116+
if ($request instanceof Request) {
117+
$data['tags']['route'] = $request->attributes->get('_route');
118+
}
119+
114120
$this->eventDispatcher->dispatch(SentrySymfonyEvents::PRE_CAPTURE, $event);
115-
$this->client->captureException($exception);
121+
$this->client->captureException($exception, $data);
116122
}
117123

118124
/**

test/EventListener/ExceptionListenerTest.php

+42-12
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,24 @@ class ExceptionListenerTest extends TestCase
4242

4343
private $mockEventDispatcher;
4444

45-
private $mockRequestStack;
45+
/**
46+
* @var RequestStack
47+
*/
48+
private $requestStack;
4649

4750
public function setUp()
4851
{
4952
$this->mockTokenStorage = $this->createMock(TokenStorageInterface::class);
5053
$this->mockAuthorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
5154
$this->mockSentryClient = $this->createMock(\Raven_Client::class);
5255
$this->mockEventDispatcher = $this->createMock(EventDispatcherInterface::class);
53-
$this->mockRequestStack = $this->createMock(RequestStack::class);
56+
$this->requestStack = new RequestStack();
5457

5558
$containerBuilder = new ContainerBuilder();
5659
$containerBuilder->setParameter('kernel.root_dir', 'kernel/root');
5760
$containerBuilder->setParameter('kernel.environment', 'test');
5861

59-
$containerBuilder->set('request_stack', $this->mockRequestStack);
62+
$containerBuilder->set('request_stack', $this->requestStack);
6063
$containerBuilder->set('security.token_storage', $this->mockTokenStorage);
6164
$containerBuilder->set('security.authorization_checker', $this->mockAuthorizationChecker);
6265
$containerBuilder->set('sentry.client', $this->mockSentryClient);
@@ -404,15 +407,9 @@ public function test_that_ip_is_set_from_request_stack()
404407

405408
$mockEvent = $this->createMock(GetResponseEvent::class);
406409

407-
$mockRequest = $this->createMock(Request::class);
408-
409-
$mockRequest
410-
->method('getClientIp')
411-
->willReturn('1.2.3.4');
412-
413-
$this->mockRequestStack
414-
->method('getCurrentRequest')
415-
->willReturn($mockRequest);
410+
$this->requestStack->push(new Request([], [], [], [], [], [
411+
'REMOTE_ADDR' => '1.2.3.4',
412+
]));
416413

417414
$mockEvent
418415
->expects($this->once())
@@ -510,6 +507,39 @@ public function test_that_it_captures_exception()
510507
$listener->onKernelException($mockEvent);
511508
}
512509

510+
public function test_that_it_captures_exception_with_route()
511+
{
512+
$reportableException = new \Exception();
513+
514+
$mockEvent = $this->createMock(GetResponseForExceptionEvent::class);
515+
$mockEvent
516+
->expects($this->once())
517+
->method('getException')
518+
->willReturn($reportableException);
519+
520+
$this->mockEventDispatcher
521+
->expects($this->once())
522+
->method('dispatch')
523+
->with($this->identicalTo(SentrySymfonyEvents::PRE_CAPTURE), $this->identicalTo($mockEvent));
524+
525+
$data = [
526+
'tags' => [
527+
'route' => 'homepage',
528+
],
529+
];
530+
531+
$this->requestStack->push(new Request([], [], ['_route' => 'homepage']));
532+
533+
$this->mockSentryClient
534+
->expects($this->once())
535+
->method('captureException')
536+
->with($this->identicalTo($reportableException), $data);
537+
538+
$this->containerBuilder->compile();
539+
$listener = $this->getListener();
540+
$listener->onKernelException($mockEvent);
541+
}
542+
513543
/**
514544
* @dataProvider mockCommandProvider
515545
*/

0 commit comments

Comments
 (0)