Skip to content

Commit 1d67b6d

Browse files
author
oxcom
committed
isse #9: add support for console error and exception events. no tests.
1 parent 4e68d8d commit 1d67b6d

File tree

11 files changed

+177
-15
lines changed

11 files changed

+177
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
/coverage/
33
/vendor/
44

5+
phpunit.xml
56
composer.lock
6-
clover.xml
7+
clover.xml

EventListener/AbstractListener.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SymfonyRollbarBundle\EventListener;
44

5+
use Symfony\Component\Console\ConsoleEvents;
56
use Symfony\Component\DependencyInjection\ContainerInterface;
67
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
78
use Monolog\Logger;
@@ -69,16 +70,32 @@ public function getLogger()
6970
*/
7071
public static function getSubscribedEvents()
7172
{
72-
return [
73-
KernelEvents::EXCEPTION => ['onKernelException', 1],
73+
$events = [
74+
KernelEvents::EXCEPTION => ['onKernelException', -100],
7475
];
76+
77+
if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
78+
$key = class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')
79+
? ConsoleEvents::ERROR
80+
: ConsoleEvents::EXCEPTION;
81+
82+
$events[$key] = ['onConsoleError', -100];
83+
}
84+
85+
return $events;
7586
}
7687

7788
/**
7889
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
7990
*/
8091
abstract public function onKernelException(GetResponseForExceptionEvent $event);
8192

93+
/**
94+
* @param \Symfony\Component\Console\Event\ConsoleErrorEvent
95+
* |\Symfony\Component\Console\Event\ConsoleExceptionEvent $event
96+
*/
97+
abstract public function onConsoleError($event);
98+
8299
/**
83100
* @return \Symfony\Component\DependencyInjection\ContainerInterface
84101
*/

EventListener/ErrorListener.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SymfonyRollbarBundle\EventListener;
44

55
use Rollbar\ErrorWrapper;
6+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
67
use Symfony\Component\DependencyInjection\ContainerInterface;
78
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
89
use SymfonyRollbarBundle\DependencyInjection\SymfonyRollbarExtension;
@@ -106,4 +107,13 @@ public function onKernelException(GetResponseForExceptionEvent $event)
106107
{
107108
// dummy
108109
}
110+
111+
/**
112+
* @param \Symfony\Component\Console\Event\ConsoleErrorEvent
113+
* |\Symfony\Component\Console\Event\ConsoleExceptionEvent $event
114+
*/
115+
public function onConsoleError($event)
116+
{
117+
// dummy
118+
}
109119
}

EventListener/ExceptionListener.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace SymfonyRollbarBundle\EventListener;
44

5+
use Symfony\Component\Console\ConsoleEvents;
6+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
7+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
58
use Symfony\Component\DependencyInjection\ContainerInterface;
69
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
710

@@ -63,4 +66,27 @@ public function handleException($exception)
6366
'payload' => $payload,
6467
]);
6568
}
69+
70+
/**
71+
* @param \Symfony\Component\Console\Event\ConsoleErrorEvent
72+
* |\Symfony\Component\Console\Event\ConsoleExceptionEvent $event
73+
*/
74+
public function onConsoleError($event)
75+
{
76+
if (class_exists('\Symfony\Component\Console\Event\ConsoleErrorEvent')
77+
&& $event instanceof ConsoleErrorEvent
78+
) {
79+
$exception = $event->getError();
80+
$this->handleException($exception);
81+
} elseif (class_exists('\Symfony\Component\Console\Event\ConsoleExceptionEvent')
82+
&& $event instanceof ConsoleExceptionEvent
83+
) {
84+
$exception = $event->getException();
85+
if ($exception instanceof \Exception
86+
|| (version_compare(PHP_VERSION, '7.0.0') >= 0 && $exception instanceof \Error)
87+
) {
88+
$this->handleException($exception);
89+
}
90+
}
91+
}
6692
}

Resources/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ services:
1111
class: "%symfony_rollbar.event_listener.exception_listener.class%"
1212
arguments: ["@service_container"]
1313
tags:
14-
- { name: kernel.event_listener, event: kernel.exception, priority: -100 }
14+
- { name: kernel.event_subscriber }
1515

1616
symfony_rollbar.event_listener.error_listener:
1717
class: "%symfony_rollbar.event_listener.error_listener.class%"
1818
arguments: ["@service_container"]
1919
tags:
20-
- { name: kernel.event_listener, event: kernel.exception }
20+
- { name: kernel.event_subscriber }
2121

2222
symfony_rollbar.provider.api_client:
2323
class: "%symfony_rollbar.provider.api_client.class%"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Tests\Fixtures;
4+
5+
6+
class MyExceptionCommand
7+
{
8+
9+
}

Tests/SymfonyRollbarBundle/DependencyInjection/SymfonyRollbarExtensionTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,56 @@ public function testConfigEnabledVars($var, $value)
4242
public function generatorConfigVars()
4343
{
4444
$exclude = Configuration::$exclude;
45+
$defaultErrorMask = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
46+
47+
$default = [
48+
'enable' => true,
49+
'exclude' => $exclude,
50+
'rollbar' => [
51+
'access_token' => '',
52+
'agent_log_location' => '%kernel.logs_dir%/rollbar.log',
53+
'base_api_url' => 'https://api.rollbar.com/api/1/',
54+
'branch' => Configuration::BRANCH,
55+
'capture_error_stacktraces' => true,
56+
'checkIgnore' => null,
57+
'code_version' => '',
58+
'enable_utf8_sanitization' => true,
59+
'environment' => Configuration::ENVIRONMENT,
60+
'error_sample_rates' => [],
61+
'handler' => Configuration::HANDLER_BLOCKING,
62+
'include_error_code_context' => false,
63+
'include_exception_code_context' => false,
64+
'included_errno' => $defaultErrorMask,
65+
'logger' => null,
66+
'person' => [],
67+
'person_fn' => null,
68+
'root' => '%kernel.root_dir%',
69+
'scrub_fields' => Configuration::$scrubFieldsDefault,
70+
'shift_function' => true,
71+
'timeout' => 3,
72+
'report_suppressed' => false,
73+
'use_error_reporting' => false,
74+
'proxy' => null,
75+
'allow_exec' => true,
76+
'endpoint' => 'https://api.rollbar.com/api/1/',
77+
'custom' => [],
78+
'exception_sample_rates' => [],
79+
'fluent_host' => '127.0.0.1',
80+
'fluent_port' => 24224,
81+
'fluent_tag' => 'rollbar',
82+
'host' => null,
83+
'scrub_whitelist' => null,
84+
'send_message_trace' => false,
85+
'include_raw_request_body' => false,
86+
'local_vars_dump' => false,
87+
],
88+
];
4589

4690
return [
4791
['symfony_rollbar.event_listener.exception_listener.class', ExceptionListener::class],
4892
['symfony_rollbar.event_listener.error_listener.class', ErrorListener::class],
4993
['symfony_rollbar.provider.rollbar_handler.class', RollbarHandler::class],
50-
['symfony_rollbar.config', ['enable' => true, 'exclude' => $exclude]],
94+
['symfony_rollbar.config', $default],
5195
];
5296
}
5397

Tests/SymfonyRollbarBundle/EventListener/AbstractListenerTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace SymfonyRollbarBundle\Tests\EventListener;
33

44
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
5+
use Symfony\Component\Console\ConsoleEvents;
56
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
67
use Symfony\Component\HttpKernel\KernelEvents;
78
use SymfonyRollbarBundle\EventListener\AbstractListener;
@@ -49,8 +50,17 @@ public function testGetSubscribedEvents($class)
4950
$listener = new $class($container);
5051

5152
$expect = [
52-
KernelEvents::EXCEPTION => ['onKernelException', 1],
53+
KernelEvents::EXCEPTION => ['onKernelException', -100],
5354
];
55+
56+
if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
57+
$key = class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')
58+
? ConsoleEvents::ERROR
59+
: ConsoleEvents::EXCEPTION;
60+
61+
$expect[$key] = ['onConsoleError', -100];
62+
}
63+
5464
$list = $listener::getSubscribedEvents();
5565

5666
$this->assertEquals($expect, $list);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"test": [
5151
"composer install",
5252
"./vendor/bin/phpcs --standard=psr2 DependencyInjection/ EventListener/ Provider/ Tests/SymfonyRollbarBundle",
53-
"./vendor/bin/phpunit -c Tests/phpunit.xml.dist"
53+
"./vendor/bin/phpunit -c phpunit.xml.dist"
5454
]
5555
}
5656
}

phpunit.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
bootstrap="./Tests/Fixtures/app/autoload.php"
10+
>
11+
<listeners>
12+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
13+
</listeners>
14+
15+
<php>
16+
<ini name="error_reporting" value="-1" />
17+
<env name="APP_DEBUG" value="false" />
18+
<server name="KERNEL_DIR" value="Fixtures/app" />
19+
<server name="KERNEL_CLASS" value="AppKernel" />
20+
</php>
21+
22+
<testsuites>
23+
<testsuite name="Project Test Suite">
24+
<directory>Tests/</directory>
25+
</testsuite>
26+
</testsuites>
27+
28+
<filter>
29+
<whitelist>
30+
<directory>./../</directory>
31+
<exclude>
32+
<directory>./../Resources</directory>
33+
<directory>./../Tests</directory>
34+
<directory>./../vendor</directory>
35+
</exclude>
36+
</whitelist>
37+
</filter>
38+
<!--<logging>-->
39+
<!--<log type="coverage-html"-->
40+
<!--target="./../coverage"-->
41+
<!--lowUpperBound="35"-->
42+
<!--highLowerBound="70" />-->
43+
<!--<log type="coverage-clover" target="../clover.xml" />-->
44+
<!--</logging>-->
45+
</phpunit>

0 commit comments

Comments
 (0)