Skip to content

Commit

Permalink
ExceptionListener > Add route tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Dec 7, 2018
1 parent c0154ea commit 15b70d4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/EventListener/ExceptionListener.php
Expand Up @@ -111,8 +111,14 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
return;
}

$data = ['tags' => []];
$request = $this->requestStack->getCurrentRequest();
if ($request instanceof Request) {
$data['tags']['route'] = $request->attributes->get('_route');
}

$this->eventDispatcher->dispatch(SentrySymfonyEvents::PRE_CAPTURE, $event);
$this->client->captureException($exception);
$this->client->captureException($exception, $data);
}

/**
Expand Down
54 changes: 42 additions & 12 deletions test/EventListener/ExceptionListenerTest.php
Expand Up @@ -42,21 +42,24 @@ class ExceptionListenerTest extends TestCase

private $mockEventDispatcher;

private $mockRequestStack;
/**
* @var RequestStack
*/
private $requestStack;

public function setUp()
{
$this->mockTokenStorage = $this->createMock(TokenStorageInterface::class);
$this->mockAuthorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
$this->mockSentryClient = $this->createMock(\Raven_Client::class);
$this->mockEventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->mockRequestStack = $this->createMock(RequestStack::class);
$this->requestStack = new RequestStack();

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

$containerBuilder->set('request_stack', $this->mockRequestStack);
$containerBuilder->set('request_stack', $this->requestStack);
$containerBuilder->set('security.token_storage', $this->mockTokenStorage);
$containerBuilder->set('security.authorization_checker', $this->mockAuthorizationChecker);
$containerBuilder->set('sentry.client', $this->mockSentryClient);
Expand Down Expand Up @@ -404,15 +407,9 @@ public function test_that_ip_is_set_from_request_stack()

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

$mockRequest = $this->createMock(Request::class);

$mockRequest
->method('getClientIp')
->willReturn('1.2.3.4');

$this->mockRequestStack
->method('getCurrentRequest')
->willReturn($mockRequest);
$this->requestStack->push(new Request([], [], [], [], [], [
'REMOTE_ADDR' => '1.2.3.4',
]));

$mockEvent
->expects($this->once())
Expand Down Expand Up @@ -510,6 +507,39 @@ public function test_that_it_captures_exception()
$listener->onKernelException($mockEvent);
}

public function test_that_it_captures_exception_with_route()
{
$reportableException = new \Exception();

$mockEvent = $this->createMock(GetResponseForExceptionEvent::class);
$mockEvent
->expects($this->once())
->method('getException')
->willReturn($reportableException);

$this->mockEventDispatcher
->expects($this->once())
->method('dispatch')
->with($this->identicalTo(SentrySymfonyEvents::PRE_CAPTURE), $this->identicalTo($mockEvent));

$data = [
'tags' => [
'route' => 'homepage',
],
];

$this->requestStack->push(new Request([], [], ['_route' => 'homepage']));

$this->mockSentryClient
->expects($this->once())
->method('captureException')
->with($this->identicalTo($reportableException), $data);

$this->containerBuilder->compile();
$listener = $this->getListener();
$listener->onKernelException($mockEvent);
}

/**
* @dataProvider mockCommandProvider
*/
Expand Down

0 comments on commit 15b70d4

Please sign in to comment.