/
ExceptionListener.php
218 lines (183 loc) · 6.72 KB
/
ExceptionListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Sentry\SentryBundle\EventListener;
use Sentry\SentryBundle\Event\SentryUserContextEvent;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Class ExceptionListener
* @package Sentry\SentryBundle\EventListener
*/
class ExceptionListener implements SentryExceptionListenerInterface
{
/** @var TokenStorageInterface|null */
private $tokenStorage;
/** @var AuthorizationCheckerInterface|null */
private $authorizationChecker;
/** @var \Raven_Client */
protected $client;
/** @var EventDispatcherInterface */
protected $eventDispatcher;
/** @var RequestStack */
private $requestStack;
/** @var string[] */
protected $skipCapture;
/**
* ExceptionListener constructor.
* @param \Raven_Client $client
* @param EventDispatcherInterface $dispatcher
* @param array $skipCapture
* @param TokenStorageInterface|null $tokenStorage
* @param AuthorizationCheckerInterface|null $authorizationChecker
*/
public function __construct(
\Raven_Client $client,
EventDispatcherInterface $dispatcher,
RequestStack $requestStack,
array $skipCapture,
TokenStorageInterface $tokenStorage = null,
AuthorizationCheckerInterface $authorizationChecker = null
) {
$this->client = $client;
$this->eventDispatcher = $dispatcher;
$this->requestStack = $requestStack;
$this->skipCapture = $skipCapture;
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}
/**
* @param \Raven_Client $client
*/
public function setClient(\Raven_Client $client)
{
$this->client = $client;
}
/**
* Set the username from the security context by listening on core.request
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event): void
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
if (null === $this->tokenStorage || null === $this->authorizationChecker) {
return;
}
$token = $this->tokenStorage->getToken();
if (null !== $token && $token->isAuthenticated() && $this->authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)) {
$this->setUserValue($token->getUser());
$contextEvent = new SentryUserContextEvent($token);
$this->eventDispatcher->dispatch(SentrySymfonyEvents::SET_USER_CONTEXT, $contextEvent);
}
}
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event): void
{
$exception = $event->getException();
if ($this->shouldExceptionCaptureBeSkipped($exception)) {
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, $data);
}
/**
* This method only ensures that the client and error handlers are registered at the start of the command
* execution cycle, and not only on exceptions
*
* @param ConsoleCommandEvent $event
*
* @return void
*/
public function onConsoleCommand(ConsoleCommandEvent $event): void
{
// only triggers loading of client, does not need to do anything.
}
public function onConsoleError(ConsoleErrorEvent $event): void
{
$this->handleConsoleError($event);
}
public function onConsoleException(ConsoleExceptionEvent $event): void
{
$this->handleConsoleError($event);
}
/**
* @param ConsoleExceptionEvent|ConsoleErrorEvent $event
*/
private function handleConsoleError(ConsoleEvent $event): void
{
$command = $event->getCommand();
switch (true) {
case $event instanceof ConsoleErrorEvent:
$exception = $event->getError();
break;
case $event instanceof ConsoleExceptionEvent:
$exception = $event->getException();
break;
default:
throw new \InvalidArgumentException('Event not recognized: ' . \get_class($event));
}
if ($this->shouldExceptionCaptureBeSkipped($exception)) {
return;
}
$data = [
'tags' => [
'command' => $command ? $command->getName() : 'N/A',
'status_code' => $event->getExitCode(),
],
];
$this->eventDispatcher->dispatch(SentrySymfonyEvents::PRE_CAPTURE, $event);
$this->client->captureException($exception, $data);
}
protected function shouldExceptionCaptureBeSkipped(\Throwable $exception): bool
{
foreach ($this->skipCapture as $className) {
if ($exception instanceof $className) {
return true;
}
}
return false;
}
/**
* @param UserInterface | object | string $user
*/
private function setUserValue($user)
{
$data = [];
$request = $this->requestStack->getCurrentRequest();
if ($request instanceof Request) {
$data['ip_address'] = $request->getClientIp();
}
if ($user instanceof UserInterface) {
$this->client->set_user_data($user->getUsername(), null, $data);
return;
}
if (is_string($user)) {
$this->client->set_user_data($user, null, $data);
return;
}
if (is_object($user) && method_exists($user, '__toString')) {
$this->client->set_user_data((string)$user, null, $data);
}
}
}