symfony/error-handler
Symfony ErrorHandler provides robust PHP error and exception handling with better debugging tools. Enable debug mode, convert warnings/notices into exceptions, and safely wrap code execution with ErrorHandler::call, even bypassing the @ silencer.
Whoops, Debugbar, or Laravel’s built-in exception handler). It can act as a drop-in replacement or complementary layer for debugging and error management.
app.debug mode with richer stack traces, variable inspection, and interactive error pages (e.g., Symfony’s Debug class).ErrorHandler::call() provides a programmatic way to execute risky code blocks (e.g., file operations, external API calls) while capturing suppressed errors, which aligns with Laravel’s try-catch patterns but with framework-backed reliability.@method annotations) without noise from third-party libraries, critical for Laravel’s PHP 8.1+ roadmap.HttpClient, Process), this package standardizes error handling across the stack, reducing fragmentation.Whoops by registering Symfony’s Debug class as the default error handler in App\Exceptions\Handler.HtmlErrorRenderer::setTemplate() can override Laravel’s resources/views/errors/ views.artisan commands with interactive debugging (e.g., Debug::enable() in bootstrap/app.php).Debug::enable() in AppServiceProvider). Advanced use cases (e.g., custom error pages) add ~20 lines.try-catch blocks with ErrorHandler::call() for idempotent operations (e.g., file caching, API retries).report() method in App\Exceptions\Handler to log errors via Symfony’s ErrorHandler before delegating to Monolog/Sentry.APP_DEBUG=false) to avoid exposing sensitive data. Use HtmlErrorRenderer::setTemplate() to customize production errors.@method) may surface in CI/CD. Filter these via ErrorHandler::setDeprecationListener().view() helper to render Symfony templates if needed.Debug mode replace Laravel’s Whoops/Debugbar, or complement it (e.g., enable both in dev)?ErrorHandler::call() be used globally (e.g., in a base service class) or selectively (e.g., for file operations)?app.debug mode with Symfony’s Debug class. Register in App\Exceptions\Handler:
if (app()->environment('local')) {
\Symfony\Component\ErrorHandler\Debug::enable();
}
\Symfony\Component\ErrorHandler\HtmlErrorRenderer::setTemplate(__DIR__.'/../resources/views/errors/custom.php');
bootstrap/app.php for Artisan commands:
if ($this->environment === 'local') {
\Symfony\Component\ErrorHandler\Debug::enable();
}
report() before rendering with Symfony’s handler.Debug (e.g., enable both in dev).Phase 1: Debug Mode (Low Risk)
composer.json:
composer require symfony/error-handler
AppServiceProvider:
if (app()->isLocal()) {
\Symfony\Component\ErrorHandler\Debug::enable();
}
localhost:8000/debug.Phase 2: Error Rendering (Medium Risk)
resources/views/errors/custom.php):
<?php use Symfony\Component\ErrorHandler\HtmlErrorRenderer; ?>
<?= HtmlErrorRenderer::render($exception, 500) ?>
App\Exceptions\Handler:
public function render($request, Throwable $exception)
{
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
return parent::render($request, $exception);
}
return response()->view('errors.custom', ['exception' => $exception], 500);
}
Phase 3: Programmatic Error Handling (High Impact)
try-catch blocks with ErrorHandler::call():
$data = \Symfony\Component\ErrorHandler\ErrorHandler::call(function () {
return json_decode(file_get_contents('config.json'), true);
});
@file_get_contents()) now throw exceptions.Phase 4: Deprecation Management (Optional)
\Symfony\Component\ErrorHandler\ErrorHandler::register();
\Symfony\Component\ErrorHandler\DebugClassLoader::enable();
symfony/error-handler:^6.4 (PHP 7.4+).How can I help you explore Laravel packages today?