Skip to content

Commit

Permalink
update: allow to work with php7
Browse files Browse the repository at this point in the history
  • Loading branch information
OxCom committed Sep 9, 2017
1 parent 371bb43 commit a7690e3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
47 changes: 35 additions & 12 deletions src/Payload/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,54 @@ public function __construct(ContainerInterface $container)
*
* @return array
*/
public function getExceptionPayload(\Exception $exception)
public function getExceptionPayload($exception)
{
// handle exception
$chain = new TraceChain();
$item = new TraceItem();

$data = $item($exception);
$message = $data['exception']['message'];

/**
* Build payload
* @link https://rollbar.com/docs/api/items_post/
*/
$payload = [
'body' => ['trace_chain' => $chain($exception)],
'request' => $this->getRequestInfo(),
'environment' => $this->getKernel()->getEnvironment(),
'body' => [],
'framework' => \Symfony\Component\HttpKernel\Kernel::VERSION,
'language_version' => phpversion(),
'server' => $this->getServerInfo(),
'language_version' => phpversion(),
'request' => $this->getRequestInfo(),
'environment' => $this->getKernel()->getEnvironment(),
];

// @link http://php.net/manual/en/reserved.constants.php
// @link http://php.net/manual/en/language.errors.php7.php
if (!($exception instanceof \Exception) || PHP_MAJOR_VERSION > 7 && !($exception instanceof \Throwable)) {
$payload['body'] = $this->buildGeneratorError($exception, __FILE__, __LINE__);

return ['Undefined error', $payload];
}

// handle exception
$chain = new TraceChain();
$item = new TraceItem();

$data = $item($exception);
$message = $data['exception']['message'];
$payload['body'] = ['trace_chain' => $chain($exception)];

return [$message, $payload];
}

/**
* @param $object
* @param $file
* @param $line
*
* @return array
*/
protected function buildGeneratorError($object, $file, $line)
{
$item = new ErrorItem();

return ['trace' => $item(0, serialize($object), $file, $line)];
}

/**
* @param int $code
* @param string $message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace Tests\SymfonyRollbarBundle\EventListener;

use Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
use \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use SymfonyRollbarBundle\EventListener\AbstractListener;

/**
* Class ExceptionListenerTest
Expand Down
38 changes: 36 additions & 2 deletions tests/SymfonyRollbarBundle/Payload/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function testGetContainer()
$container = static::$kernel->getContainer();
$generator = $container->get('symfony_rollbar.payload.generator');

$ctnr = $generator->getContainer();
$result = $generator->getContainer();

$this->assertEquals($container, $ctnr);
$this->assertEquals($container, $result);
}

public function testGetKernel()
Expand Down Expand Up @@ -198,4 +198,38 @@ public function testGetExceptionPayload()
$this->assertEquals(phpversion(), $payload['language_version']);
$this->assertEquals($serverInfo, $payload['server']);
}

/**
* @dataProvider generatorStrangeData
* @param mixed $data
*/
public function testStrangeException($data)
{
/**
* @var \SymfonyRollbarBundle\Payload\Generator $generator
*/
$container = static::$kernel->getContainer();
$generator = $container->get('symfony_rollbar.payload.generator');

list($message, $payload) = $generator->getExceptionPayload($data);

$this->assertEquals('Undefined error', $message);
$this->assertNotEmpty($payload['body']['trace']);
}

/**
* @return array
*/
public function generatorStrangeData()
{
return [
['zxcv'],
[1234],
[0.2345],
[null],
[(object)['p' => 'a']],
[['s' => 'app', 'd' => 'web']],
[new ErrorItem()],
];
}
}

0 comments on commit a7690e3

Please sign in to comment.