Weave 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

Api Exception Bundle Laravel Package

m6web/api-exception-bundle

Symfony bundle that centralizes API exception handling and returns clean JSON responses. Supports configurable error formatting, stack traces, form validation errors, exception matching/mapping, logging, and environment-specific behavior.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Symfony2 Bundle in Laravel Context: The package is designed for Symfony2’s event-driven architecture, which differs fundamentally from Laravel’s middleware-based exception handling. While the core concept (structured JSON error responses) aligns well with Laravel APIs, the implementation requires significant adaptation due to:
    • Laravel’s App\Exceptions\Handler (replaces Symfony’s EventListener).
    • Laravel’s Illuminate\Validation (replaces Symfony’s Validator).
    • Laravel’s service container (replaces Symfony’s DependencyInjection).
  • Key Strengths for Laravel:
    • Standardized error payloads improve API consumer DX (e.g., frontend teams, third-party integrations).
    • Configurable stack trace exposure aligns with Laravel’s APP_DEBUG mode.
    • Validation error handling can integrate with Laravel’s FormRequest validation.
  • Key Weaknesses:
    • No native Laravel support: Requires rewriting or forking.
    • Outdated dependencies: Symfony2 components may conflict with modern Laravel versions.
    • Lack of Laravel-specific features: No built-in support for Laravel’s Nova, Sanctum, or Horizon integrations.

Integration Feasibility

  • Direct Integration: Not feasible due to Symfony2’s kernel and DI container being incompatible with Laravel.
  • Indirect Integration:
    • High: Core logic (exception formatting, JSON responses) can be reimplemented in Laravel with minimal effort.
    • Medium: Advanced features (e.g., event listeners for validation errors) require custom middleware or service providers.
  • Critical Dependencies to Replace:
    Symfony Component Laravel Equivalent Adaptation Complexity
    HttpKernel Illuminate\Foundation\Http Low
    EventDispatcher Illuminate\Events Medium
    Validator Illuminate\Validation\Validator Low
    Twig Blade (irrelevant for APIs) N/A
    DependencyInjection Illuminate\Container Medium

Technical Risk

Risk Factor Impact Assessment Mitigation Strategy
Symfony2 Deprecation Bundle relies on unsupported Symfony2 components (e.g., HttpKernel). Abstract dependencies into Laravel-compatible interfaces or use Symfony5+ bridges.
Lack of Maintenance Last release in 2019; no Laravel-specific updates. Fork the repo and maintain a Laravel branch, or rewrite only the needed logic.
Configuration Drift Symfony’s YAML/XML config → Laravel’s PHP arrays. Convert config to config/exceptions.php with Laravel’s config() helper.
Testing Gaps No Laravel test suite; potential edge cases in validation/error handling. Write Pest/PHPUnit tests for adapted exception classes.
Performance Overhead Symfony’s event system may add latency if not optimized. Use Laravel middleware (lighter than Symfony events) for exception handling.
Validation Integration Symfony’s Validator → Laravel’s Validator may have API differences. Test validation error payloads against Laravel’s FormRequest validation.

Key Questions for TPM

  1. Is the goal to adopt this bundle as-is or adapt its core logic?
    • As-is: Not recommended (Symfony2 incompatibility).
    • Adapt: Justifiable if the standardized error format is a high priority.
  2. What’s the tradeoff between effort and value?
    • Low effort: Use Laravel’s built-in App\Exceptions\Handler for basic JSON responses.
    • High effort: Adapt the bundle for advanced features (e.g., stack trace control, validation errors).
  3. Are there existing Laravel alternatives?
  4. Will this integrate with Laravel’s ecosystem?
    • Example: Does it work with Laravel Sanctum (API auth) or Nova (admin panel)?
  5. What’s the long-term maintenance plan?
    • If adopting, fork the repo and commit to updates.
    • If rewriting, ensure the solution is framework-agnostic (e.g., standalone PHP library).

Integration Approach

Stack Fit

  • Laravel Compatibility Matrix:

    Feature Laravel Equivalent Integration Path
    Exception Handling App\Exceptions\Handler Override render() to return JSON responses.
    Validation Errors Illuminate\Validation\Validator Extend FormRequest or use Validator::make() in exception logic.
    Event-Driven Logic Illuminate\Events Replace Symfony’s EventDispatcher with Laravel’s event() helper.
    HTTP Responses Illuminate\Http\JsonResponse Use response()->json() instead of Symfony’s JsonResponse.
    Configuration config/exceptions.php Convert YAML/XML to Laravel’s PHP array format.
    Dependency Injection Illuminate\Container Bind exceptions/services in AppServiceProvider.
    Logging Illuminate\Log Use Laravel’s Log facade instead of Symfony’s LoggerInterface.
  • Recommended Stack:

    • Laravel 8/9/10 (Symfony 5+ compatible).
    • PHP 8.0+ (for modern exception handling).
    • Pest/PHPUnit for testing adapted logic.

Migration Path

Option 1: Lightweight Adaptation (Recommended for Most Teams)

  1. Step 1: Define Laravel-Compatible Exception Classes (1 day)

    • Create app/Exceptions/ApiException.php:
      namespace App\Exceptions;
      use Exception;
      use Illuminate\Http\JsonResponse;
      
      class ApiException extends Exception {
          public function toJson(): JsonResponse {
              return response()->json([
                  'error' => [
                      'code' => $this->getCode(),
                      'message' => $this->getMessage(),
                      'status' => $this->httpStatusCode ?? 500,
                      'details' => $this->details ?? null,
                  ],
              ], $this->httpStatusCode);
          }
      }
      
    • Extend for validation errors: app/Exceptions/ValidationException.php.
  2. Step 2: Override App\Exceptions\Handler (1 day)

    • Modify render() to catch exceptions and return JSON:
      public function render($request, Throwable $exception) {
          if ($exception instanceof \App\Exceptions\ApiException) {
              return $exception->toJson();
          }
          return parent::render($request, $exception);
      }
      
  3. Step 3: Add Configuration (0.5 days)

    • Create config/exceptions.php:
      return [
          'codes' => [
              'validation' => 422,
              'not_found' => 404,
          ],
          'debug' => [
              'show_stack_trace' => env('APP_DEBUG'),
          ],
      ];
      
  4. Step 4: Test Edge Cases (1 day)

    • Test with:
      • Custom exceptions (throw new ApiException("Test", 400)).
      • Validation errors ($validator->errors()->toJson()).
      • Debug vs. production modes (APP_DEBUG).

Option 2: Full Bundle Fork (For Teams with Symfony Expertise)

  1. Step 1: Fork the Repository
    • Clone and rename to laravel-api-exception-bundle.
  2. Step 2: Replace Symfony Dependencies
    • Use symfony/http-foundation bridge for JsonResponse.
    • Replace EventDispatcher with Laravel’s Events.
  3. Step 3: Adapt Configuration
    • Convert YAML to PHP arrays.
  4. Step 4: Publish as Composer Package
    • Host on GitHub/Packagist for team reuse.

Compatibility

  • Laravel Versions:
    • Supported: Laravel 8+ (Symfony 5+ compatible).
    • Unsupported: Laravel <7 (Symfony 4/5 may conflict).
  • PHP Versions:
    • Minimum: PHP 8.0 (for modern exception handling).
    • Recommended: PHP 8.1+ (for attributes,
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.
dakiquang/laravel-oauth-refresh-lock
woda/laravel-worktrees
dalefranks/ramasite
faghy/laracashbook
roundly-consulting/google-places-for-laravel
roundly-consulting/kubernetes-api-for-laravel
roundly-consulting/plausible-for-laravel
gvp/gvp-laravel-template
metasyncsite/laravel-metasync-client
roundly-consulting/onboarding-for-laravel
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
adachsoft/changelog-linter