Skip to content

Commit 0498ee0

Browse files
author
Simon Bouland
committed
Exception listener with dedicated data methods
Issue #175
1 parent a4054b4 commit 0498ee0

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/EventListener/ExceptionListener.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,24 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
111111
return;
112112
}
113113

114+
$this->eventDispatcher->dispatch(SentrySymfonyEvents::PRE_CAPTURE, $event);
115+
$this->client->captureException($exception, $this->getExceptionData());
116+
}
117+
118+
/**
119+
* Additional attributes to pass with this event (see Sentry docs).
120+
*
121+
* @return array
122+
*/
123+
protected function getExceptionData()
124+
{
114125
$data = ['tags' => []];
115126
$request = $this->requestStack->getCurrentRequest();
116127
if ($request instanceof Request) {
117128
$data['tags']['route'] = $request->attributes->get('_route');
118129
}
119130

120-
$this->eventDispatcher->dispatch(SentrySymfonyEvents::PRE_CAPTURE, $event);
121-
$this->client->captureException($exception, $data);
131+
return $data;
122132
}
123133

124134
/**
@@ -188,9 +198,11 @@ protected function shouldExceptionCaptureBeSkipped(\Throwable $exception): bool
188198
}
189199

190200
/**
191-
* @param UserInterface | object | string $user
201+
* Additional user data
202+
*
203+
* @return array
192204
*/
193-
protected function setUserValue($user)
205+
protected function getUserData()
194206
{
195207
$data = [];
196208

@@ -199,20 +211,28 @@ protected function setUserValue($user)
199211
$data['ip_address'] = $request->getClientIp();
200212
}
201213

214+
return $data;
215+
}
216+
217+
/**
218+
* @param UserInterface | object | string $user
219+
*/
220+
protected function setUserValue($user)
221+
{
202222
if ($user instanceof UserInterface) {
203-
$this->client->set_user_data($user->getUsername(), null, $data);
223+
$this->client->set_user_data($user->getUsername(), null, $this->getUserData());
204224

205225
return;
206226
}
207227

208228
if (is_string($user)) {
209-
$this->client->set_user_data($user, null, $data);
229+
$this->client->set_user_data($user, null, $this->getUserData());
210230

211231
return;
212232
}
213233

214234
if (is_object($user) && method_exists($user, '__toString')) {
215-
$this->client->set_user_data((string)$user, null, $data);
235+
$this->client->set_user_data((string)$user, null, $this->getUserData());
216236
}
217237
}
218238
}

0 commit comments

Comments
 (0)