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

Exception listener with dedicated data methods #180

Merged
merged 1 commit into from Jan 16, 2019
Merged
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
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());
}
}
}