Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setBeforeNotifyFunction is not working. #12

Closed
Jamesking56 opened this issue Mar 8, 2014 · 4 comments
Closed

setBeforeNotifyFunction is not working. #12

Jamesking56 opened this issue Mar 8, 2014 · 4 comments

Comments

@Jamesking56
Copy link

Hello,

I have a Laravel 4.1 application and I have added BugSnag-Laravel to it.

Unfortunately, the setBeforeNotifyFunction isn't working.

Here is my function I created in my helpers.php file which is autoloaded using composer:

function bugsnagBefore($error)
{
    $data = ['user' => null];

    if (\Auth::check()) {
        $data['user'] = [
            'name' => \Auth::user()->username,
            'email' => \Auth::user()->email
        ];
    } else {
        $data['user'] = [
            'name' => 'Guest'
        ];
    }

    $error->setMetaData($data);
}

helpers.php is autoloaded in composer:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "files": [
        "app/LicenseApp/helpers.php"
    ]
},

Then, I told BugSnag about my function in my App::before filter:

App::before(function($request)
{
    \Bugsnag::setBeforeNotifyFunction("bugsnagBefore");
});

This however, doesn't work. The extra information doesn't appear in BugSnag. Any reason why?

@snmaynard
Copy link
Contributor

Is there nothing in the logs that show what went wrong?

We use call_user_func to call the function, so if the function isnt a global we wont be able to call it. If you add a log message into the function to double check it is being successfully called it should help scope the issue.

@drekinov
Copy link

@Jamesking56 could you try different approach to set beforeNotify function. I am not sure that composer autoloader is correct way.

My implementation is to create app/bugsnag_init.php where i define a function and set all additional settings for Bugsnag PHP which are not supported through Bugsnag Service Provider:

$app['bugsnag']->setBeforeNotifyFunction("before_bugsnag_notify");
$app['bugsnag']->setType("laravel");
if (!function_exists('before_bugsnag_notify')) {
    function before_bugsnag_notify(Bugsnag_Error $error) { .... } 

Moreover you should include that file in app/start/global.php like app/filters.php is included.
Probably that implementation is possible through additional service provider too.

Moreover in staging and production you probably would return some user friendly page for generic not catched exceptions in:

App::error(function (Exception $exception, $code) { ... }

If App::error return Response::view then Bugsnag does not log any error or exception so be sure that App::error has Bugsnag::notifyException($exception); before returning Response::view, in case app.debug is false.
I hope that helps :)

@pajcho
Copy link

pajcho commented Mar 25, 2014

@Jamesking56 Here is how I created it and I can confirm this works:

I created new file just for bugsnag, but your helpers.php will do the job.
Include helpers.php file in app/start/global.php

// Put this at end of global.php file
require app_path().'/LicenseApp/helpers.php';

Your helpers.php file should look like this:

function bugsnagBefore($error)
{
    $data = ['user' => null];
    if (\Auth::check()) {
        $data['user'] = [
            'name' => \Auth::user()->username,
            'email' => \Auth::user()->email
        ];
    } else {
        $data['user'] = [
            'name' => 'Guest'
        ];
    }
    $error->setMetaData($data);
}

Bugsnag::setBeforeNotifyFunction("bugsnagBefore");

And that's it!! Just invoke any Exception and it will work!!

Hope this helps you.

@snmaynard
Copy link
Contributor

@Jamesking56 have you resolved this now? Going to close but feel free to re-open & comment if you are still having issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants