Skip to content

Commit 2cf1cfd

Browse files
author
Andrii Afanasiev
committed
fix: issue #4 - extend support for PHP7+
1 parent fffce2e commit 2cf1cfd

File tree

9 files changed

+63
-13
lines changed

9 files changed

+63
-13
lines changed

EventListener/ExceptionListener.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ public function onKernelException(GetResponseForExceptionEvent $event)
4242
*/
4343
public function handleException($exception)
4444
{
45-
if ($exception instanceof \Exception && $this->handler->shouldSkip($exception)) {
45+
if (($exception instanceof \Exception || $exception instanceof \Throwable)
46+
&& $this->handler->shouldSkip($exception)
47+
) {
4648
return;
4749
}
4850

4951
$payload = [];
52+
5053
// @link http://php.net/manual/en/reserved.constants.php
5154
// @link http://php.net/manual/en/language.errors.php7.php
52-
if (!($exception instanceof \Exception) || PHP_MAJOR_VERSION > 7 && !($exception instanceof \Throwable)) {
55+
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
5356
$payload = ['message' => @serialize($exception)];
5457
$exception = new \Exception('Undefined exception');
5558
}

Provider/ApiClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ public function __construct(ContainerInterface $container)
4848
}
4949

5050
/**
51-
* @TODO: inject mocked clienty
51+
* @TODO : inject mocked clienty
5252
*
5353
* @param array $payload
5454
*
5555
* @link https://rollbar.com/docs/api/deploys/
56+
* @return \Psr\Http\Message\ResponseInterface
5657
*/
5758
public function trackBuild($payload = [])
5859
{
59-
$this->client->post('deploy', [
60+
return $this->client->post('deploy', [
6061
'form_params' => $payload,
6162
]);
6263
}

Provider/RollbarHandler.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ protected function write(array $record)
113113
'datetime' => $record['datetime']->format('U'),
114114
]);
115115

116-
if (isset($context['exception']) && $context['exception'] instanceof \Exception) {
116+
if (isset($context['exception'])
117+
&& ($context['exception'] instanceof \Exception || $context['exception'] instanceof \Throwable)
118+
) {
117119
$payload['level'] = $context['level'];
118120
$exception = $context['exception'];
119121
unset($context['exception']);
@@ -131,11 +133,11 @@ protected function write(array $record)
131133
}
132134

133135
/**
134-
* @param \Exception $exception
136+
* @param \Throwable $exception
135137
*
136138
* @return bool
137139
*/
138-
public function shouldSkip(\Exception $exception)
140+
public function shouldSkip($exception)
139141
{
140142
// check exception
141143
foreach ($this->exclude as $instance) {
@@ -172,15 +174,15 @@ public function close()
172174
* @param string $rollbarUser
173175
* @param string $localUser
174176
*
175-
* @return bool
177+
* @return null|\Psr\Http\Message\ResponseInterface
176178
*/
177179
public function trackBuild($environment, $revision, $comment = '', $rollbarUser = '', $localUser = '')
178180
{
179181
// There is no API in Rollbar SDK for tracking builds
180182
$config = $this->getContainer()->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
181183

182184
if (!$config['enable']) {
183-
return false;
185+
return null;
184186
}
185187

186188
/** @var \SymfonyRollbarBundle\Provider\ApiClient $client */

Tests/Fixtures/ErrorHandler.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public static function getInstance()
2727
}
2828

2929
return static::$instance;
30+
31+
3032
}
3133

3234
/**
@@ -46,10 +48,10 @@ public function setAssert($assert = null)
4648
*/
4749
protected function write(array $record)
4850
{
49-
$dummy = function () {
50-
};
51+
if (!empty($this->assert)) {
52+
$cb = $this->assert;
5153

52-
$closure = empty($this->assert) ? $dummy : $this->assert;
53-
call_user_func($closure, $record);
54+
$cb($record);
55+
}
5456
}
5557
}

Tests/Fixtures/app/config/config_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ symfony_rollbar:
77
exclude:
88
- \Symfony\Component\Debug\Exception\FatalErrorException
99
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
10+
- \ParseError
1011
rollbar:
1112
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1213
environment: '%kernel.environment%'

Tests/SymfonyRollbarBundle/Command/DeployCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function testRegistration()
3131

3232
return;
3333
}
34+
35+
// it's should not be risky test
36+
$this->assertTrue(true);
3437
}
3538

3639
/**

Tests/SymfonyRollbarBundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testParameters()
2222

2323
$exclude = Configuration::$exclude;
2424
$exclude[] = '\SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException';
25+
$exclude[] = '\ParseError';
2526

2627
$default = [
2728
'enable' => true,

Tests/SymfonyRollbarBundle/EventListener/ExceptionListenerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,39 @@ public function testSkipException()
172172
$this->assertTrue(true); // trick to mark not risky
173173
restore_error_handler();
174174
}
175+
176+
/**
177+
* @dataProvider generatorHandleParams
178+
* @param $data
179+
*/
180+
public function testHandleParams(\Throwable $data)
181+
{
182+
$container = static::$kernel->getContainer();
183+
184+
$handler = new ErrorHandler();
185+
$handler->setAssert(function ($record) use ($data) {
186+
$this->assertNotEmpty($record);
187+
188+
$this->assertEquals($data->getMessage(), $record['message']);
189+
$this->assertEquals(Logger::ERROR, $record['level']);
190+
$this->assertNotEmpty($record['context']['exception']);
191+
192+
$exception = $record['context']['exception'];
193+
$this->assertEquals($data, $exception);
194+
});
195+
196+
$listener = new ExceptionListener($container);
197+
$listener->getLogger()->setHandlers([$handler]);
198+
199+
$listener->handleException($data);
200+
}
201+
202+
public function generatorHandleParams()
203+
{
204+
return [
205+
[new \Exception('This is new exception')],
206+
[new \Exception('This is one more new exception')],
207+
[new \TypeError('This is TypeError')],
208+
];
209+
}
175210
}

Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public function generatorShouldSkip()
147147
[new \Exception(), false],
148148
[new \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException(), true],
149149
[new \Symfony\Component\Debug\Exception\UndefinedFunctionException("error", $e), true],
150+
[new \TypeError("TypeError"), false],
151+
[new \ParseError("ParseError"), true],
150152
];
151153
}
152154

0 commit comments

Comments
 (0)