Skip to content

Commit

Permalink
Add a processor to remove all stacktraces from reported exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ste93cry committed Mar 24, 2017
1 parent 801367e commit 7ac62fd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Collect User.ip_address automatically (#419).
- Added a processor to remove web cookies and another to remove HTTP body data for POST, PUT, PATCH and DELETE requests. They will be enabled by default in ``2.0`` (#405).
- Added a processor to sanitize HTTP headers (e.g. the Authorization header) (#428).
- Added a processor to remove all stacktraces from reported exceptions (#429)

1.6.2
-----
Expand Down
39 changes: 39 additions & 0 deletions lib/Raven/Processor/RemoveStacktraceProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* This processor removes the stacktrace from all exceptions captured by an
* event.
*
* @author Stefano Arlandini <[email protected]>
*/
class Raven_Processor_RemoveStacktraceProcessor extends Raven_Processor
{
/**
* {@inheritdoc}
*/
public function process(&$data)
{
if (!isset($data['exception'], $data['exception']['values'])) {
return;
}

foreach ($data['exception']['values'] as &$exception) {
if (!isset($exception['stacktrace'])) {
continue;
}

foreach ($exception['stacktrace']['frames'] as &$frame) {
unset($frame['pre_context'], $frame['context_line'], $frame['post_context']);
}
}
}
}
82 changes: 82 additions & 0 deletions test/Raven/Tests/Processor/RemoveStacktraceProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Raven_Tests_RemoveStacktraceProcessorTest extends PHPUnit_Framework_TestCase
{
/**
* @var Raven_Client|PHPUnit_Framework_MockObject_MockObject
*/
protected $client;

/**
* @var Raven_Processor_RemoveStacktraceProcessor
*/
protected $processor;

protected function setUp()
{
$this->client = $this->getMockBuilder('Raven_Client')
->setMethodsExcept(array('captureException', 'capture', 'get_default_data', 'get_http_data', 'get_user_data', 'get_extra_data'))
->getMock();

$this->client->store_errors_for_bulk_send = true;

$this->processor = new Raven_Processor_RemoveStacktraceProcessor($this->client);
}

public function testProcess()
{
try {
throw new \Exception();
} catch (\Exception $exception) {
$this->client->captureException($exception);
}

$this->assertArrayHasKey('stacktrace', $this->client->_pending_events[0]['exception']['values'][0]);

$this->processor->process($this->client->_pending_events[0]);

$this->assertArrayNotHasKey('pre_context', $this->client->_pending_events[0]['exception']['values'][0]['stacktrace']['frames']);
$this->assertArrayNotHasKey('context_line', $this->client->_pending_events[0]['exception']['values'][0]['stacktrace']['frames']);
$this->assertArrayNotHasKey('post_context', $this->client->_pending_events[0]['exception']['values'][0]['stacktrace']['frames']);
}

public function testProcessWithPreviousException()
{
try {
try {
throw new \Exception('foo');
} catch (\Exception $exception) {
throw new \Exception('bar', 0, $exception);
}
} catch (\Exception $exception) {
$this->client->captureException($exception);
}

foreach ($this->client->_pending_events[0]['exception']['values'] as $exceptionValue) {
foreach ($exceptionValue['stacktrace']['frames'] as $frame) {
$this->assertArrayHasKey('pre_context', $frame);
$this->assertArrayHasKey('context_line', $frame);
$this->assertArrayHasKey('post_context', $frame);
}
}

$this->processor->process($this->client->_pending_events[0]);

foreach ($this->client->_pending_events[0]['exception']['values'] as $exceptionValue) {
foreach ($exceptionValue['stacktrace']['frames'] as $frame) {
$this->assertArrayNotHasKey('pre_context', $frame);
$this->assertArrayNotHasKey('context_line', $frame);
$this->assertArrayNotHasKey('post_context', $frame);
}
}
}
}

0 comments on commit 7ac62fd

Please sign in to comment.