Skip to content

Commit

Permalink
hande symfony exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Afanasiev committed Apr 26, 2017
1 parent aea7e3c commit d03c504
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.idea
/vendor/

/var/
!/var/.gitkeep

composer.lock
composer.phar
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"psr-4": { "SymfonyRollbarBundle\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Tests\\": "Tests/"}
"psr-4": { "Tests\\": "tests/"}
},
"require": {
"php": ">=5.6",
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="Tests/Fixtures/app//autoload.php"
bootstrap="tests/Fixtures/app/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="Tests/Fixtures/app" />
<server name="KERNEL_DIR" value="tests/Fixtures/app" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>Tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/AbstractListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Monolog\Logger;
use Monolog\Handler\RollbarHandler;
use SymfonyRollbarBundle\DependencyInjection\SymfonyRollbarExtension;
use SymfonyRollbarBundle\Formatter\ExceptionFormatter;
use SymfonyRollbarBundle\Formatter\Generator;

/**
* Class AbstractListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Symfony\Component\HttpKernel\KernelEvents;

class Error extends \SymfonyRollbarBundle\EventListener\AbstractListener
class ErrorListener extends AbstractListener
{
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
// KernelEvents::EXCEPTION => ['onKernelException', 1],
KernelEvents::EXCEPTION => ['onKernelException', 1],
];
}
}
20 changes: 13 additions & 7 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use SymfonyRollbarBundle\Formatter\ExceptionFormatter;
use SymfonyRollbarBundle\Payload\Generator;

class ExceptionListener extends \SymfonyRollbarBundle\EventListener\AbstractListener
class ExceptionListener extends AbstractListener
{
/**
* Process exception
Expand All @@ -16,12 +16,18 @@ class ExceptionListener extends \SymfonyRollbarBundle\EventListener\AbstractList
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$formatter = new ExceptionFormatter();
$payload = $formatter->format($exception);

$this->getLogger()->error($payload['message'], [
'payload' => $payload['trace_chain'],
]);
if ($exception instanceof \Exception) {
// generate payload and log data
$generator = new Generator($this->container);
$request = $event->getRequest();

list($message, $payload) = $generator->getPayload($exception, $request);

$this->getLogger()->error($message, [
'payload' => $payload,
]);
}
}

/**
Expand Down
30 changes: 0 additions & 30 deletions src/Formatter/ExceptionFormatter.php

This file was deleted.

75 changes: 75 additions & 0 deletions src/Payload/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace SymfonyRollbarBundle\Payload;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

class Generator
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

/**
* Get payload a log record.
*
* @param \Exception $exception
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return array
*/
public function getPayload(\Exception $exception, Request $request)
{
// handle exception
$chain = new TraceChain();
$item = new TraceItem();
$kernel = $this->getContainer()->get('kernel');

$data = $item($exception);
$message = $data['exception']['message'];
$args = isset($_SERVER['argv']) ? $_SERVER['argv'] : [];

/**
* Build payload
* @link https://rollbar.com/docs/api/items_post/
*/
$payload = [
'body' => ['trace_chain' => $chain($exception)],
'request' => [
'url' => $request->getRequestUri(),
'method' => $request->getMethod(),
'headers' => $request->headers->all(),
'query_string' => $request->getQueryString(),
'body' => $request->getContent(),
'user_ip' => $request->getClientIp(),
],
'environment' => $kernel->getEnvironment(),
'framework' => \Symfony\Component\HttpKernel\Kernel::VERSION,
'language_version' => phpversion(),
'server' => [
'host' => gethostname(),
'root' => $kernel->getRootDir(),
'user' => get_current_user(),
'file' => array_shift($args),
'argv' => implode(' ', $args),
],
];

return [$message, $payload];
}

/**
* @return \Symfony\Component\DependencyInjection\ContainerInterface
*/
public function getContainer()
{
return $this->container;
}
}
4 changes: 3 additions & 1 deletion src/Formatter/TraceChain.php → src/Payload/TraceChain.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

namespace SymfonyRollbarBundle\Formatter;
namespace SymfonyRollbarBundle\Payload;

class TraceChain
{
/**
* @param \Exception $exception
*
* @return array
*/
public function __invoke(\Exception $exception)
Expand Down
15 changes: 8 additions & 7 deletions src/Formatter/TraceItem.php → src/Payload/TraceItem.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace SymfonyRollbarBundle\Formatter;

use Monolog\Formatter\FormatterInterface;
namespace SymfonyRollbarBundle\Payload;

class TraceItem
{
/**
* @param \Exception $exception
*
* @return array
*/
public function __invoke(\Exception $exception)
{
$frames = [];
Expand All @@ -27,8 +30,7 @@ public function __invoke(\Exception $exception)
// build method
$method = empty($row['function']) ? null : $row['function'];
$call = empty($row['type']) ? '::' : $row['type'];
$args = '(' . implode(', ', $frame['args']) . ')';
$frame['method'] = $frame['class_name'] . $call . $method . $args;
$frame['method'] = $frame['class_name'] . $call . $method . '()';

$frames[] = $frame;
}
Expand All @@ -37,8 +39,7 @@ public function __invoke(\Exception $exception)
'exception' => [
'class' => get_class($exception),
'message' => implode(' ', [
'Exception',
"'" . get_class($exception) . "'",
"'\\" . get_class($exception) . "'",
'with message',
"'" . $exception->getMessage() . "'",
'occurred in file',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,32 @@ public function registerBundles()
return $bundles;
}

/**
* @return string
*/
public function getRootDir()
{
return __DIR__;
}

/**
* @param \Symfony\Component\Config\Loader\LoaderInterface $loader
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}

/**
* @return string
*/
public function getCacheDir()
{
return realpath(__DIR__ . '/../../../') . '/var/' . $this->environment . '/cache';
}

public function getLogDir()
{
return realpath(__DIR__ . '/../../../') . '/var/' . $this->environment . '/logs';
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function testBoot()
* @var TraceableEventDispatcher $eventDispatcher
*/
$eventDispatcher = $container->get('event_dispatcher');
$listeners = $eventDispatcher->getListeners();
// $listeners = $eventDispatcher->getListeners();

$exception = new \Exception('This is new report');
$event = new GetResponseForExceptionEvent(
static::$kernel, new Request(),
Expand Down
Empty file added var/.gitkeep
Empty file.

0 comments on commit d03c504

Please sign in to comment.