Sentry-laravel: Add setUser() instructions to README

Created on 20 Jan 2019  路  9Comments  路  Source: getsentry/sentry-laravel

The Sentry docs for capturing the user include PHP but not Laravel so this:

configureScope(function (Scope $scope): void {
    $scope->setUser(['email' => '[email protected]']);
});

Had me very confused. For Laravel the first line needs to be:

app('sentry')->configureScope(function (Scope $scope): void {

It would be helpful to include this right in the README. Something like this:

        use Sentry\State\Scope;
        use function Sentry\configureScope;

        if (app()->bound('sentry') && $this->shouldReport($exception)) {
            app('sentry')->configureScope(function (Scope $scope): void {
                $user = auth()->user();

                if ($user) {
                    $scope->setUser([
                        'id' => $user->id,
                        'email' => $user->email,
                        'plan' => $user->plan
                    ]);
                }
            });
            app('sentry')->captureException($exception);
        }

I'll also be suggesting they add it to the installation page.

Most helpful comment

use function Sentry\configureScope;

configureScope(function (Scope $scope): void {
  $scope->setUser(['email' => '[email protected]']);
});

This should work even in the Laravel integration right @HazAT? Or are we doing something funky? But yes either way we should add documentation for this! Thanks for opening an issue for that!

All 9 comments

use function Sentry\configureScope;

configureScope(function (Scope $scope): void {
  $scope->setUser(['email' => '[email protected]']);
});

This should work even in the Laravel integration right @HazAT? Or are we doing something funky? But yes either way we should add documentation for this! Thanks for opening an issue for that!

Definitely didn't work for me and I had the correct use statements. I posted this on r/laravel because it wasn't working. This comment may explain why?

A note about configuring releases in the config would also be helpful. Thanks for responding!

futzlarson solution does work, but its important to note that depending on where the exception happens, the user may not be available/loaded yet in the process and thus unavailable to the error handler.

You may need to modify the kernel.php as per answer here https://stackoverflow.com/questions/34982471/accessing-auth-in-exceptions-handler-php to make it available in the handler

I also had to add
use Sentry\State\Scope;
to the top of the handler.php

@TLMNicolas I check auth()->user() before adding user information. Wouldn't that suffice, or does it need to be auth()->check()? I've never been sure which one to use.

That SO post is over two years old so I figured it no longer applied.

I did have use Sentry\State\Scope as well, just didn't include. I updated my original post.

I had a problem in my situation (with 5.7) that the scope object type was a different type than the one that was coming in (had to check the php logs to see the error), probably an autoloader issue, so by explicitly use-ing it fixed that one problem for me.

Then I had a problem where the auth()->user (aka Auth::user()) was null.
I was testing with bad php in a page controller (just put xyz in a line in the php).
I would not get the user's data in my sentry, so after some dirty debugging, i found the user was not loaded at that point, which lead me to the SO answer - specifically moving the 3 lines in the "update" part of the SO answer.

I think the simple check for if($user) is fine, and it works fine, no error there.
From my understanding, auth()->check() is simply to check if the user is authenticated.

After re-reading your post, did you get this working or are you looking for assistance? (if the latter Ill try what I can)

For what it's worth, I'm on 5.6.

Rearranging middleware - at least at this stage of my Laravel understanding - seems risky. Like it's in a specific order for a reason, right?

Side question: Is there any different between if (auth()->user()) and if (auth()->check())?

I did get it working. This issue is merely to suggest some updates to the README to make it easier for newcomers because that threw me for an unnecessary loop. A note about configuring releases (in the config, commented out by default) would also be nice. Really anything in the Sentry Getting Started progress bar:

I'm not sure if I should just comment it here or add a new issue. Because my problem is not about setUser exactly but using configureScope. But since they are very similar as I saw here, I will post my problem here first.

The code I'm doing here is an Artisan command. I want to catch an Exception (so the command still running after that), but I want to send all exceptions to Sentry log service.

This is how my code looks like:

// First, I import Sentry dependencies...
use Sentry\State\Scope;
use function Sentry\captureException;
use function Sentry\configureScope;

// .... then, later in my code, there is the try/catch block:
try {
      // a few things here...

} catch (\Exception $e) {
    // create some extra values to help my debugging flow:
    // Obs: $schedule is a variable previously created in the script:
    configureScope(function (Scope $scope) use($schedule) {
        $scope->setExtra('player1', $schedule->player1->name);
        $scope->setExtra('player2', $schedule->player2->name);
        $scope->setExtra('tournament_event_id', $schedule->event->id);
    });

    // send the exception to Sentry: 
    app('sentry')->captureException($e);
}

Running that, I get this error:

Call to undefined function Sentry\configureScope()

I try to install the default Sentry SDK, but it conflicts with the one Sentry-Laravel is using:

composer require sentry/sentry:2.0.0-beta1 php-http/curl-client guzzlehttp/psr7
Using version ^1.7 for php-http/curl-client
Using version ^1.5 for guzzlehttp/psr7
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - sentry/sentry-laravel 0.11.0 requires sentry/sentry ^1.9.0 -> satisfiable by sentry/sentry[1.x-dev].
    - sentry/sentry-laravel 0.11.0 requires sentry/sentry ^1.9.0 -> satisfiable by sentry/sentry[1.x-dev].
    - sentry/sentry-laravel 0.11.0 requires sentry/sentry ^1.9.0 -> satisfiable by sentry/sentry[1.x-dev].
    - Can only install one of: sentry/sentry[2.0.0-beta1, 1.x-dev].
    - Installation request for sentry/sentry 2.0.0-beta1 -> satisfiable by sentry/sentry[2.0.0-beta1].
    - Installation request for sentry/sentry-laravel ^0.11.0 -> satisfiable by sentry/sentry-laravel[0.11.0].


Installation failed, reverting ./composer.json to its original content.

I would say a new issue. Mine is just about updating the README. Best to keep it separate.

The 1.0.0 version of the Laravel integration is now released.

Installation instructions are here: docs.sentry.io/platforms/php/laravel

This also includes instructions for adding user context: https://docs.sentry.io/platforms/php/laravel/#user-context

If it's still unclear please open a new issue so we can address that in the documentation!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jstolp picture jstolp  路  4Comments

vladislavtkachenko picture vladislavtkachenko  路  4Comments

AidasK picture AidasK  路  5Comments

errogaht picture errogaht  路  8Comments

Dalabad picture Dalabad  路  3Comments