bugsnag/bugsnag-laravel

Invalid API Key fatal error, registerCallback in AppServiceProvider::boot()

markkimsal opened this issue · 5 comments

New installation, try to add meta data with Bugsnag::registerCallback, trigger exception but get an Invalid API Key exception.

    public function boot()
    { 
           \Bugsnag\BugsnagLaravel\Facades\Bugsnag::registerCallback(function ($report) { 
            if (!$u = \Auth::user()) { 
                return;
            } 
            $report->setMetaData([
                'account' => [ 
                    'email' => $u->email,
                ] 
            ]);
        });
    } 
    public function register()
    {
        $this->app->singleton('bugsnag', function (Container $app) {
            $config = $app->config->get('bugsnag');
            //config is null here
            $client = new Client(new Configuration($config['api_key']), new LaravelResolver($app), $this->getGuzzle($config));

Somehow it is getting to BugSnagServiceProvider::register() before boot and setupConfig()

It does work later, just not in appserviceprovider::boot, where the docs say to put it.

Route::get('/testexception', function(\Request $request) { 
        \Bugsnag\BugsnagLaravel\Facades\Bugsnag::registerCallback(function ($report) { 
            if (!$u = \Auth::user()) { 
                return;
            }
            $report->setMetaData([
                'account' => [ 
                    'email' => $u->email,
                ]
            ]);
        });

    $ex =  new \Exception('test bugsnag');
   \Bugsnag\BugsnagLaravel\Facades\Bugsnag::notifyException($ex);
});

Hi @markkimsal what is the method you are using for setting your API key?

The error you are seeing is typically thrown if the api_key is not known when trying to set up the Client. The guidelines for setting up your API key are available here: https://docs.bugsnag.com/platforms/php/laravel/configuration-options/#api-key

I set it in the .env according to the documentation at
https://docs.bugsnag.com/platforms/php/laravel/#basic-configuration

This page states that publishing the vendor config is optional, but I don't believe it is.

 \Bugsnag\BugsnagLaravel\Facades\Bugsnag::registerCallback(function ($report) { });

This code works in routes/web.php but it dies in app/Providers/AppServiceProviders.php::boot()

@markkimsal I have managed to reproduce the 'Invalid API Key' error. This is happening for me when I register Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class after the App\Providers\AppServiceProvider::class in config/app.php.

As mentioned in the Laravel Installation documentation, it is important to register our service provider before your AppServiceProvider.

Do you think this is what you are seeing?

@markkimsal I have managed to reproduce the 'Invalid API Key' error. This is happening for me when I register Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class after the App\Providers\AppServiceProvider::class in config/app.php.

As mentioned in the Laravel Installation documentation, it is important to register our service provider before your AppServiceProvider.

Do you think this is what you are seeing?

This is what fixed it for me. Thanks