Skip to content

Add support for exception groups #1812

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
use Sentry\Transport\Result;
use Sentry\Transport\TransportInterface;

use function count;

/**
* Default implementation of the {@see ClientInterface} interface.
*/
@@ -468,10 +470,24 @@
$exceptions[] = new ExceptionDataBag(
$exception,
$this->stacktraceBuilder->buildFromException($exception),
// @FIXME this sets the same mechanism on all exception
$hint->mechanism ?? new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, true, ['code' => $exception->getCode()])
);
} while ($exception = $exception->getPrevious());

if (count($exceptions) > 1) {
foreach ($exceptions as $key => $value) {
if ($key === 0) {
$value->getMechanism()->setIsExceptionGroup(true);

Check failure on line 481 in src/Client.php

GitHub Actions / PHPStan

Cannot call method setIsExceptionGroup() on Sentry\ExceptionMechanism|null.
}
if ($key > 0) {
$value->getMechanism()->setParentId(0);

Check failure on line 484 in src/Client.php

GitHub Actions / PHPStan

Cannot call method setParentId() on Sentry\ExceptionMechanism|null.
}

$value->getMechanism()->setExceptionId($key);

Check failure on line 487 in src/Client.php

GitHub Actions / PHPStan

Cannot call method setExceptionId() on Sentry\ExceptionMechanism|null.
}
}

$event->setExceptions($exceptions);
}
}
45 changes: 45 additions & 0 deletions src/ExceptionMechanism.php
Original file line number Diff line number Diff line change
@@ -32,6 +32,12 @@
*/
private $data;

private $exceptionId;

Check failure on line 35 in src/ExceptionMechanism.php

GitHub Actions / PHPStan

Property Sentry\ExceptionMechanism::$exceptionId has no type specified.

private $parentId;

Check failure on line 37 in src/ExceptionMechanism.php

GitHub Actions / PHPStan

Property Sentry\ExceptionMechanism::$parentId has no type specified.

private $isExceptionGroup;

Check failure on line 39 in src/ExceptionMechanism.php

GitHub Actions / PHPStan

Property Sentry\ExceptionMechanism::$isExceptionGroup has no type specified.

/**
* Class constructor.
*
@@ -47,6 +53,9 @@
$this->type = $type;
$this->handled = $handled;
$this->data = $data;
$this->exceptionId = 0;
$this->parentId = null;
$this->isExceptionGroup = false;
}

/**
@@ -89,4 +98,40 @@

return $this;
}

public function getExceptionId(): int
{
return $this->exceptionId;
}

public function setExceptionId(int $exceptionId): self
{
$this->exceptionId = $exceptionId;

return $this;
}

public function getParentId(): ?int
{
return $this->parentId;
}

public function setParentId(int $parentId): self
{
$this->parentId = $parentId;

return $this;
}

public function getIsExceptionGroup(): bool
{
return $this->isExceptionGroup;
}

public function setIsExceptionGroup(bool $isExceptionGroup): self
{
$this->isExceptionGroup = $isExceptionGroup;

return $this;
}
}
13 changes: 11 additions & 2 deletions src/Serializer/EnvelopItems/EventItem.php
Original file line number Diff line number Diff line change
@@ -165,25 +165,34 @@ public static function toEnvelopeItem(Event $event): string
*/
protected static function serializeException(ExceptionDataBag $exception): array
{
$exceptionMechanism = $exception->getMechanism();
$exceptionStacktrace = $exception->getStacktrace();
$result = [
'type' => $exception->getType(),
'value' => $exception->getValue(),
];

$exceptionStacktrace = $exception->getStacktrace();
if ($exceptionStacktrace !== null) {
$result['stacktrace'] = [
'frames' => array_map([self::class, 'serializeStacktraceFrame'], $exceptionStacktrace->getFrames()),
];
}

$exceptionMechanism = $exception->getMechanism();
if ($exceptionMechanism !== null) {
$result['mechanism'] = [
'type' => $exceptionMechanism->getType(),
'handled' => $exceptionMechanism->isHandled(),
'exception_id' => $exceptionMechanism->getExceptionId(),
];

if ($exceptionMechanism->getIsExceptionGroup()) {
$result['mechanism']['is_exception_group'] = true;
}

if ($exceptionMechanism->getParentId() !== null) {
$result['mechanism']['parent_id'] = $exceptionMechanism->getParentId();
}

if ($exceptionMechanism->getData() !== []) {
$result['mechanism']['data'] = $exceptionMechanism->getData();
}
Loading