Skip to content

Commit

Permalink
Exception listener with dedicated data methods
Browse files Browse the repository at this point in the history
Issue #175
  • Loading branch information
Simon Bouland committed Jan 9, 2019
1 parent a4054b4 commit 0498ee0
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/EventListener/ExceptionListener.php
Expand Up @@ -111,14 +111,24 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
return;
}

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

/**
* Additional attributes to pass with this event (see Sentry docs).
*
* @return array
*/
protected function getExceptionData()
{
$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);
return $data;
}

/**
Expand Down Expand Up @@ -188,9 +198,11 @@ protected function shouldExceptionCaptureBeSkipped(\Throwable $exception): bool
}

/**
* @param UserInterface | object | string $user
* Additional user data
*
* @return array
*/
protected function setUserValue($user)
protected function getUserData()
{
$data = [];

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

return $data;
}

/**
* @param UserInterface | object | string $user
*/
protected function setUserValue($user)
{
if ($user instanceof UserInterface) {
$this->client->set_user_data($user->getUsername(), null, $data);
$this->client->set_user_data($user->getUsername(), null, $this->getUserData());

return;
}

if (is_string($user)) {
$this->client->set_user_data($user, null, $data);
$this->client->set_user_data($user, null, $this->getUserData());

return;
}

if (is_object($user) && method_exists($user, '__toString')) {
$this->client->set_user_data((string)$user, null, $data);
$this->client->set_user_data((string)$user, null, $this->getUserData());
}
}
}

0 comments on commit 0498ee0

Please sign in to comment.