Skip to content

Commit

Permalink
do not auto-attach a backtrace when logging an exception
Browse files Browse the repository at this point in the history
this fixes #957.

The sentry server prefers the event's stacktrace over an attached
exception's stacktrace, so if a stacktrace is attached to an event
containing an exception, the web interface would show the backtrace to
where the exception was captured rather than the exception's backtrace.
  • Loading branch information
pilif committed Jan 17, 2020
1 parent 161bd50 commit 8fc7b60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ private function prepareEvent(array $payload, ?Scope $scope = null, bool $withSt
return null;
}

if ($withStacktrace) {
// when 'exception' is set, no backtrace should be auto-attached (#957)
if (
$withStacktrace
&& !isset($payload['exception'])
&& !isset($payload['stacktrace'])
) {
$event = $this->eventFactory->createWithStacktrace($payload);
} else {
$event = $this->eventFactory->create($payload);
Expand Down
13 changes: 8 additions & 5 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testCaptureEvent(): void
/**
* @dataProvider captureEventAttachesStacktraceAccordingToAttachStacktraceOptionDataProvider
*/
public function testCaptureEventAttachesStacktraceAccordingToAttachStacktraceOption(bool $shouldAttachStacktrace): void
public function testCaptureEventAttachesStacktraceAccordingToAttachStacktraceOption(bool $attachConfigSetting, array $payload, bool $shouldAttachStacktrace): void
{
/** @var TransportInterface&MockObject $transport */
$transport = $this->createMock(TransportInterface::class);
Expand All @@ -159,18 +159,21 @@ public function testCaptureEventAttachesStacktraceAccordingToAttachStacktraceOpt
}))
->willReturn('500a339f3ab2450b96dee542adf36ba7');

$client = ClientBuilder::create(['attach_stacktrace' => $shouldAttachStacktrace])
$client = ClientBuilder::create(['attach_stacktrace' => $attachConfigSetting])
->setTransportFactory($this->createTransportFactory($transport))
->getClient();

$this->assertEquals('500a339f3ab2450b96dee542adf36ba7', $client->captureEvent([]));
$this->assertEquals('500a339f3ab2450b96dee542adf36ba7', $client->captureEvent($payload));
}

public function captureEventAttachesStacktraceAccordingToAttachStacktraceOptionDataProvider(): array
{
return [
[true],
[false],
[true, [], true],
[false, [], false],
// when 'exception' is set, no backtrace should be auto-attached (#957)
[true, ['exception' => new \Exception()], false],
[false, ['exception' => new \Exception()], false],
];
}

Expand Down

0 comments on commit 8fc7b60

Please sign in to comment.