Skip to main content

Symfony Debug to Error Handler Component

·260 words·2 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

Since symfony 4.4 the symfony/debug component is deprecated.

The “\Symfony\Component\Debug\ErrorHandler” class is deprecated since Symfony 4.4, use “\Symfony\Component\ErrorHandler\ErrorHandler”

Running unit tests on Mascot, I noticed lots of warnings.

But it was not easy as changing Symfony\Component\Debug\ExceptionHandler to Symfony\Component\ErrorHandler\ErrorHandler.

Assume we have something as below

37
38
39
40
41
42
43
44
<?php
$handler = new \Symfony\Component\Debug\ExceptionHandler($debug);
$exception = $event->getThrowable();
if (!$exception instanceof FlattenException) {
    $exception = FlattenException::create($exception);
}
$response = Response::create($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders())->setCharset(ini_get('default_charset'));
$event->setResponse($response);

The Symfony\Component\ErrorHandler\ErrorHandler has no getHtml method.

From the symfony docs, if you are in development mode we can use below code.

1
2
3
4
<?php
if ($_SERVER['APP_DEBUG']) {
    Debug::enable();
}

so I started with

1
2
<?php
ErrorHandler::register();

But it was throwing all details. I was struggling how to fix this. After many failed experiments, I asked Yonel Ceruto Gonzalez.

Sets the scope at 0 to tell the handler that it is in “non-debug” mode:

$errorHandler->scopeAt(0, true);

However, don’t expect the real exception message in non-debug mode, it could reveal sensitive information.

Yonel Ceruto Gonzalez1

The above information was really helpful to do more experiments. I finally fixed the unit tests with the below code.

37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
$handler = new \Symfony\Component\ErrorHandler\ErrorHandler();
$handler->setExceptionHandler([$handler, 'renderException']);
if (! $this->debug) {
    $handler->scopeAt(0, true);
}
$exception = $event->getThrowable();
ob_start();
$handler->handleException($exception);
$response = ob_get_clean();
if (!$exception instanceof FlattenException) {
    $exception = FlattenException::create($exception);
}
$response = Response::create($response, $exception->getStatusCode(), $exception->getHeaders())->setCharset(ini_get('default_charset'));

I hope this information may help someone at somepoint of time.


  1. Taken from twitter status Yonel Ceruto Gonzalez ↩︎

Related

CakePHP ORM and Logging Queries

·196 words·1 min
Working with cakephp/orm library, I needed to log all the queries. Cakephp provides a way to do it via cakephp/log. 1 2 3 4 5 6 7 8 9 <?php use Cake\Log\Log; Log::config('queries', [ 'className' => 'File', 'path' => '/my/log/path/', 'file' => 'app', 'scopes' => ['queriesLog'] ]); But you are not limited, if you need to configure it to a PSR-3 logger like monolog/monolog 1 2 3 4 5 6 7 8 9 10 <?php use Cake\Log\Log; use Monolog\Logger; use Monolog\Handler\StreamHandler; Log::config('default', function () { $log = new Logger('cli'); $log->pushHandler(new StreamHandler('php://stdout')); return $log; }); That was pretty simple and it logs to cli.

Custom Events in a Symfony2 Bundle

·301 words·2 mins
In this tutorial we will create a custom event for symfony2 bundle. Assuming you have downloaded the symfony-standard distribution to play. Create HktEventBundle via sensio generator bundle. 1 2 <?php php app/console generate:bundle --namespace=Hkt/EventBundle --dir src --no-interaction Create the event class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php // src/Hkt/EventBundle/Event/PageViewed.php namespace Hkt\EventBundle\Event; use Symfony\Component\EventDispatcher\Event; class PageViewed extends Event { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } Add as many methods/properties which are needed from the listener. Instead of creating an event class we can make use of generic event also.

Building Modular Applications With PSR-7

·1152 words·6 mins
PSR-7, the HTTP message interfaces opened a new door of creating modular applications. Sadly many of the PSR-7 implementations added many helper methods. So if someone is creating a library that needs a PSR-7 implementations they tie the particular library with the PSR-7 implementation and use these convinient helper methods. So was PSR-15: interfaces for HTTP Middleware and PSR-17: interfaces for HTTP Factories was proposed. When creating a module one of the most challenging part is how to serve the javascript, css and images. We are going to use hkt/psr7-asset which is a fork of Aura.Asset_Bundle . What you want to do is only map the path to the assets folder.