Pixelairport's avatar

Add Sentry tags in exception

I hope there is somebody who is working with sentry and have a good idea for me. I try to add tags for some exceptions in my laravel. I know it worked in the past, but i cant remember how. This is the last thing I tried:

class MyTestException extends Exception
{
    public function __construct($message = "", $code = 0, Throwable $previous = null)
    {
        app('sentry')->withScope(function (\Sentry\State\Scope $scope) {
            $scope->setTag('saysometing','hello world');
        });

        parent::__construct($message, $code, $previous);
    }
}

I want to search later at sentry for tags.

0 likes
4 replies
Pixelairport's avatar

Yes. And I'm sure last time I did it, I also took the code and it worked. But this time it dont... But I will test it again tomorrow. Maybe I just need a break to think about it.

Pixelairport's avatar
Pixelairport
OP
Best Answer
Level 12

Ok. The following code works, but maybe have somebody an idea, if it is better to place the code anywhere else or is the constructor ok for this?

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

class MyTestException extends Exception
{
    public function __construct($message = "", $code = 0, Throwable $previous = null)
    {
        configureScope(function (Scope $scope) {
            $scope->setTag('saysomething', 'hello world');
        });

        parent::__construct($message, $code, $previous);
    }
}

Please or to participate in this conversation.