mvinayakam's avatar

Bugsnag does not report unhandled exceptions

Hello

I am able to see the errors logged via Bugsnag:NotifyException.

However it doesn't capture the unhandled exceptions which is our primary use case.

Our application is Laravel11 + Vue + Inertial

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To ensure that Bugsnag captures unhandled exceptions in your Laravel application, you need to make sure that Bugsnag is properly integrated with Laravel's exception handling system. Here are the steps to achieve this:

  1. Install Bugsnag Laravel Package: First, ensure that you have the Bugsnag Laravel package installed. You can install it via Composer:

    composer require bugsnag/bugsnag-laravel
    
  2. Configure Bugsnag: Publish the Bugsnag configuration file using the following Artisan command:

    php artisan vendor:publish --provider="Bugsnag\BugsnagLaravel\BugsnagServiceProvider"
    

    This will create a config/bugsnag.php file. Open this file and set your Bugsnag API key:

    'api_key' => env('BUGSNAG_API_KEY'),
    

    Make sure you have the BUGSNAG_API_KEY set in your .env file:

    BUGSNAG_API_KEY=your_bugsnag_api_key
    
  3. Register Bugsnag in Exception Handler: Open the app/Exceptions/Handler.php file and ensure that Bugsnag is registered in the report method. The Bugsnag Laravel package should automatically handle this, but it's good to verify:

    use Bugsnag\BugsnagLaravel\Facades\Bugsnag;
    
    class Handler extends ExceptionHandler
    {
        public function report(Throwable $exception)
        {
            if (app()->bound('bugsnag') && $this->shouldReport($exception)) {
                app('bugsnag')->notifyException($exception);
            }
    
            parent::report($exception);
        }
    }
    
  4. Check for Middleware: Ensure that Bugsnag middleware is included in your app/Http/Kernel.php file. This middleware helps in capturing unhandled exceptions:

    protected $middleware = [
        // Other middleware
        \Bugsnag\BugsnagLaravel\Middleware\UnhandledState::class,
    ];
    
  5. Verify Bugsnag Configuration: Make sure that your Bugsnag configuration is correct and that your application is running in an environment where Bugsnag is enabled (e.g., production).

  6. Test Unhandled Exceptions: To test if unhandled exceptions are being reported, you can throw an exception in one of your routes or controllers:

    Route::get('/test-bugsnag', function () {
        throw new \Exception('Test unhandled exception');
    });
    

    Access this route in your browser and check your Bugsnag dashboard to see if the exception is reported.

By following these steps, you should be able to capture unhandled exceptions in your Laravel application using Bugsnag. If you still face issues, ensure that there are no conflicts with other error handling packages or custom exception handling logic in your application.

1 like
mvinayakam's avatar

@LaryAI Super. Thanks a ton.

This works now.

I tested it with php artisan test-error and the got the command not found error in the box.

Please or to participate in this conversation.