Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExceptionListener > Add route tag #167

Merged
merged 1 commit into from Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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