Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Error Handler Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Seamless Laravel Integration: The package is designed as a standalone Symfony component but integrates effortlessly with Laravel’s existing error-handling ecosystem (e.g., 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.
    • Debug Mode: Replaces or augments Laravel’s app.debug mode with richer stack traces, variable inspection, and interactive error pages (e.g., Symfony’s Debug class).
    • Exception Wrapping: 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.
    • Deprecation Handling: Proactively surfaces PHP 8.4+ deprecation notices (e.g., @method annotations) without noise from third-party libraries, critical for Laravel’s PHP 8.1+ roadmap.
  • Modularity: The component is lightweight (~10K LOC) and decoupled, allowing TPMs to adopt only the features needed (e.g., debug mode, error rendering, or deprecation handling) without bloat.
  • Symfony Ecosystem Synergy: If the Laravel app already uses other Symfony components (e.g., HttpClient, Process), this package standardizes error handling across the stack, reducing fragmentation.

Integration Feasibility

  • Laravel Compatibility: Works out-of-the-box with Laravel 8+ (PHP 8.0+) and Symfony 6+, with backward compatibility for older versions. No Laravel-specific dependencies exist, but it augments Laravel’s native error handling.
    • Debug Mode: Can replace or extend Laravel’s Whoops by registering Symfony’s Debug class as the default error handler in App\Exceptions\Handler.
    • Error Pages: Custom templates via HtmlErrorRenderer::setTemplate() can override Laravel’s resources/views/errors/ views.
    • CLI Debugging: Enhances Laravel’s artisan commands with interactive debugging (e.g., Debug::enable() in bootstrap/app.php).
  • Minimal Boilerplate: Integration requires <5 lines of code for basic debugging (e.g., Debug::enable() in AppServiceProvider). Advanced use cases (e.g., custom error pages) add ~20 lines.
  • Existing Laravel Patterns:
    • Replaces ad-hoc try-catch blocks with ErrorHandler::call() for idempotent operations (e.g., file caching, API retries).
    • Extends Laravel’s report() method in App\Exceptions\Handler to log errors via Symfony’s ErrorHandler before delegating to Monolog/Sentry.

Technical Risk

  • Low Risk:
    • Battle-Tested: Used in Symfony, Drupal, and Laravel-adjacent projects (e.g., API Platform). No critical bugs reported in recent releases (v6.4–v8.0).
    • Backward Compatibility: Minor version updates (e.g., v7.4 → v8.0) are non-breaking for Laravel, with PHP 8.4+ features opt-in.
    • Performance: Minimal overhead in production (debug mode disabled by default). Benchmarks show <1ms latency for error rendering.
  • Mitigable Risks:
    • Debug Mode in Production: Must be disabled via environment variables (e.g., APP_DEBUG=false) to avoid exposing sensitive data. Use HtmlErrorRenderer::setTemplate() to customize production errors.
    • Deprecation Noise: Some PHP deprecation notices (e.g., @method) may surface in CI/CD. Filter these via ErrorHandler::setDeprecationListener().
    • Custom Templates: Requires Blade/PHP template files for error pages. Use Laravel’s view() helper to render Symfony templates if needed.
  • Dependencies:
    • PHP 8.0+: Required for v7.4+. Laravel 8+ meets this, but legacy apps may need upgrades.
    • No Hard Dependencies: Works alongside Laravel’s Monolog, Sentry, or custom loggers.

Key Questions for the TPM

  1. Debugging Workflow:
    • Should Symfony’s Debug mode replace Laravel’s Whoops/Debugbar, or complement it (e.g., enable both in dev)?
    • Are there specific error types (e.g., HTTP 500s, CLI failures) that need prioritized debugging?
  2. Error Exposure:
    • What sensitive data (e.g., stack traces, query logs) should be redacted in production error pages?
    • Should errors be logged to a third-party tool (e.g., Sentry) before rendering via Symfony’s handler?
  3. Integration Scope:
    • Should ErrorHandler::call() be used globally (e.g., in a base service class) or selectively (e.g., for file operations)?
    • Are there legacy error-handling patterns (e.g., custom exception classes) that need migration?
  4. Performance:
    • Will debug mode be enabled in staging environments? If so, what’s the acceptable latency for error rendering?
  5. Maintenance:
    • Who will own updates (e.g., PHP 8.4 compatibility) for this component?
    • Should deprecation notices be silenced, logged, or surfaced to developers?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Debugging: Replaces or extends Laravel’s app.debug mode with Symfony’s Debug class. Register in App\Exceptions\Handler:
      if (app()->environment('local')) {
          \Symfony\Component\ErrorHandler\Debug::enable();
      }
      
    • Error Rendering: Overrides Laravel’s error views with custom Symfony templates:
      \Symfony\Component\ErrorHandler\HtmlErrorRenderer::setTemplate(__DIR__.'/../resources/views/errors/custom.php');
      
    • CLI Debugging: Enable in bootstrap/app.php for Artisan commands:
      if ($this->environment === 'local') {
          \Symfony\Component\ErrorHandler\Debug::enable();
      }
      
  • Symfony Components:
    • If using Symfony HttpClient, Process, or Messenger, this package standardizes error handling across components.
  • Third-Party Tools:
    • Sentry/Monolog: Log errors via Laravel’s report() before rendering with Symfony’s handler.
    • Whoops/Debugbar: Can coexist with Symfony’s Debug (e.g., enable both in dev).

Migration Path

  1. Phase 1: Debug Mode (Low Risk)

    • Add to composer.json:
      composer require symfony/error-handler
      
    • Enable in AppServiceProvider:
      if (app()->isLocal()) {
          \Symfony\Component\ErrorHandler\Debug::enable();
      }
      
    • Test: Verify stack traces, variable dumps, and interactive error pages in localhost:8000/debug.
  2. Phase 2: Error Rendering (Medium Risk)

    • Create a custom error template (resources/views/errors/custom.php):
      <?php use Symfony\Component\ErrorHandler\HtmlErrorRenderer; ?>
      <?= HtmlErrorRenderer::render($exception, 500) ?>
      
    • Override Laravel’s error handler in 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);
      }
      
    • Test: Validate production error pages hide sensitive data.
  3. Phase 3: Programmatic Error Handling (High Impact)

    • Replace critical try-catch blocks with ErrorHandler::call():
      $data = \Symfony\Component\ErrorHandler\ErrorHandler::call(function () {
          return json_decode(file_get_contents('config.json'), true);
      });
      
    • Test: Ensure suppressed errors (e.g., @file_get_contents()) now throw exceptions.
  4. Phase 4: Deprecation Management (Optional)

    • Configure deprecation listeners to log or silence notices:
      \Symfony\Component\ErrorHandler\ErrorHandler::register();
      \Symfony\Component\ErrorHandler\DebugClassLoader::enable();
      

Compatibility

  • Laravel Versions:
    • Laravel 8+: Full compatibility (PHP 8.0+).
    • Laravel 7: Use symfony/error-handler:^6.4 (PHP 7.4+).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport