Skip to content

Commit a7690e3

Browse files
committed
update: allow to work with php7
1 parent 371bb43 commit a7690e3

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

src/Payload/Generator.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,54 @@ public function __construct(ContainerInterface $container)
3030
*
3131
* @return array
3232
*/
33-
public function getExceptionPayload(\Exception $exception)
33+
public function getExceptionPayload($exception)
3434
{
35-
// handle exception
36-
$chain = new TraceChain();
37-
$item = new TraceItem();
38-
39-
$data = $item($exception);
40-
$message = $data['exception']['message'];
41-
4235
/**
4336
* Build payload
4437
* @link https://rollbar.com/docs/api/items_post/
4538
*/
4639
$payload = [
47-
'body' => ['trace_chain' => $chain($exception)],
48-
'request' => $this->getRequestInfo(),
49-
'environment' => $this->getKernel()->getEnvironment(),
40+
'body' => [],
5041
'framework' => \Symfony\Component\HttpKernel\Kernel::VERSION,
51-
'language_version' => phpversion(),
5242
'server' => $this->getServerInfo(),
43+
'language_version' => phpversion(),
44+
'request' => $this->getRequestInfo(),
45+
'environment' => $this->getKernel()->getEnvironment(),
5346
];
5447

48+
// @link http://php.net/manual/en/reserved.constants.php
49+
// @link http://php.net/manual/en/language.errors.php7.php
50+
if (!($exception instanceof \Exception) || PHP_MAJOR_VERSION > 7 && !($exception instanceof \Throwable)) {
51+
$payload['body'] = $this->buildGeneratorError($exception, __FILE__, __LINE__);
52+
53+
return ['Undefined error', $payload];
54+
}
55+
56+
// handle exception
57+
$chain = new TraceChain();
58+
$item = new TraceItem();
59+
60+
$data = $item($exception);
61+
$message = $data['exception']['message'];
62+
$payload['body'] = ['trace_chain' => $chain($exception)];
63+
5564
return [$message, $payload];
5665
}
5766

67+
/**
68+
* @param $object
69+
* @param $file
70+
* @param $line
71+
*
72+
* @return array
73+
*/
74+
protected function buildGeneratorError($object, $file, $line)
75+
{
76+
$item = new ErrorItem();
77+
78+
return ['trace' => $item(0, serialize($object), $file, $line)];
79+
}
80+
5881
/**
5982
* @param int $code
6083
* @param string $message

tests/SymfonyRollbarBundle/EventListener/ExceptionListenerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22
namespace Tests\SymfonyRollbarBundle\EventListener;
33

4+
use Monolog\Logger;
45
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
56
use Symfony\Component\HttpFoundation\Request;
67
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
78
use \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
89
use Symfony\Component\HttpKernel\HttpKernelInterface;
10+
use SymfonyRollbarBundle\EventListener\AbstractListener;
911

1012
/**
1113
* Class ExceptionListenerTest

tests/SymfonyRollbarBundle/Payload/GeneratorTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function testGetContainer()
2828
$container = static::$kernel->getContainer();
2929
$generator = $container->get('symfony_rollbar.payload.generator');
3030

31-
$ctnr = $generator->getContainer();
31+
$result = $generator->getContainer();
3232

33-
$this->assertEquals($container, $ctnr);
33+
$this->assertEquals($container, $result);
3434
}
3535

3636
public function testGetKernel()
@@ -198,4 +198,38 @@ public function testGetExceptionPayload()
198198
$this->assertEquals(phpversion(), $payload['language_version']);
199199
$this->assertEquals($serverInfo, $payload['server']);
200200
}
201+
202+
/**
203+
* @dataProvider generatorStrangeData
204+
* @param mixed $data
205+
*/
206+
public function testStrangeException($data)
207+
{
208+
/**
209+
* @var \SymfonyRollbarBundle\Payload\Generator $generator
210+
*/
211+
$container = static::$kernel->getContainer();
212+
$generator = $container->get('symfony_rollbar.payload.generator');
213+
214+
list($message, $payload) = $generator->getExceptionPayload($data);
215+
216+
$this->assertEquals('Undefined error', $message);
217+
$this->assertNotEmpty($payload['body']['trace']);
218+
}
219+
220+
/**
221+
* @return array
222+
*/
223+
public function generatorStrangeData()
224+
{
225+
return [
226+
['zxcv'],
227+
[1234],
228+
[0.2345],
229+
[null],
230+
[(object)['p' => 'a']],
231+
[['s' => 'app', 'd' => 'web']],
232+
[new ErrorItem()],
233+
];
234+
}
201235
}

0 commit comments

Comments
 (0)