Skip to content

Commit 654b948

Browse files
author
Simon Bouland
committed
Exception listener with dedicated data methods
Issue #175
1 parent 353f9d3 commit 654b948

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/EventListener/ExceptionListener.php

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

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

120-
$this->eventDispatcher->dispatch(SentrySymfonyEvents::PRE_CAPTURE, $event);
121-
$this->client->captureException($exception, $data);
133+
return $data;
122134
}
123135

124136
/**
@@ -188,9 +200,11 @@ protected function shouldExceptionCaptureBeSkipped(\Throwable $exception): bool
188200
}
189201

190202
/**
191-
* @param UserInterface | object | string $user
203+
* Additional user data
204+
*
205+
* @return array
192206
*/
193-
protected function setUserValue($user)
207+
protected function getUserData()
194208
{
195209
$data = [];
196210

@@ -199,20 +213,29 @@ protected function setUserValue($user)
199213
$data['ip_address'] = $request->getClientIp();
200214
}
201215

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

205227
return;
206228
}
207229

208230
if (is_string($user)) {
209-
$this->client->set_user_data($user, null, $data);
231+
$this->client->set_user_data($user, null, $this->getUserData());
210232

211233
return;
212234
}
213235

214236
if (is_object($user) && method_exists($user, '__toString')) {
215-
$this->client->set_user_data((string)$user, null, $data);
237+
$this->client->set_user_data((string)$user, null, $this->getUserData());
216238
}
217239
}
240+
218241
}

0 commit comments

Comments
 (0)