Install the ErrorHandler component
8.2Install the component using Composer to add it to your PHP project.
composer require symfony/error-handlerrepository·8.2·Indexed 25 days ago
https://github.com/symfony/error-handlerA PHP component providing tools to manage errors and debugging. Features include error catching that bypasses the silence operator via ErrorHandler::call(), a debug class loader, custom HTML error templates via HtmlErrorRenderer, and the error:dump command for generating static error pages.
Install the component using Composer to add it to your PHP project.
composer require symfony/error-handlerYou can enable full debugging features using Debug::enable(). Alternatively, you can enable specific features individually:
ErrorHandler::register(): Registers the error handler.DebugClassLoader::enable(): Enables the debug class loader.use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\ErrorHandler\DebugClassLoader;
Debug::enable();
// or enable only one feature
//ErrorHandler::register();
//DebugClassLoader::enable();Use ErrorHandler::call() to wrap a block of code (passed as an anonymous function). If any code inside the function fails, a PHP exception will be thrown, even if the code uses the @ PHP silence operator.
use Symfony\Component\ErrorHandler\ErrorHandler;
$data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) {
// if any code executed inside this anonymous function fails, a PHP exception
// will be thrown, even if the code uses the '@' PHP silence operator
$data = json_decode(file_get_contents($filename), true);
$data['read_at'] = date($datetimeFormat);
file_put_contents($filename, json_encode($data));
return $data;
});If debug mode is not enabled, you can specify a custom generic HTML template for error rendering using HtmlErrorRenderer::setTemplate().
// If you want a custom generic template when debug is not enabled
// HtmlErrorRenderer::setTemplate('/path/to/custom/error.html.php');error:dump command generates plain HTML files for error pages based on specific HTTP status codes. These files can be directly served by a web server to provide custom error pages (like 404 or 500 errors) without requiring a live application runtime.