Giter Club home page Giter Club logo

Comments (8)

maurobonfietti avatar maurobonfietti commented on June 18, 2024

Hey @devlamine

Yes, you can do it.

Create a new project:

composer create-project maurobonfietti/slim4-api-skeleton your-api-with-sentry

Create your Sentry account, require his Sentry SDK and then follow his instructions.

composer require sentry/sdk

Then in App/ErrorHandler.php, add Sentry:

Sentry\init(['dsn' => 'YOUR_SENTRY_DSN' ]);

Sentry\captureException($exception);

For last, try throwing an exception, to test Sentry.

throw new \Exception("My first Sentry error!");

Example:

Screen Shot 2021-01-16 at 15 20 55


Keep in mind as it says in the Sentry documentation:

"To capture all errors, even the one during the startup of your application, you should initialize the Sentry PHP SDK as soon as possible."

Also, you probably want to save your DSN for Sentry in an environment variable.

Well, try all of this and tell me if it's works for you. 😄

from slim4-api-skeleton.

devlamine avatar devlamine commented on June 18, 2024

Hello @maurobonfietti ,
thanks you so very much It's work. but do I have to catch errors every time with throw new \ Exception ($message) ? because I created a test controller with a division by zero error but it's not capture by Sentry:

<?php

namespace App\Controller\User;

use Slim\Http\Request;
use Slim\Http\Response;

/**
 * Test Controller.
 */
class Test extends BaseTest
{
    /**
     * Test
     * @param Request $request
     * @param Response $response
     * @param array $args
     * @return Response
     */
    public function __invoke($request, $response, $args)
    {
        $this->setParams($request, $response, $args);
       // throw new \Exception("My first Sentry error!");

        try {
            echo 2/0 ;
        }catch(Exception $e){
            return $this->jsonResponse('error',  $e->getMessage(), 500);
        }
    }
}

from slim4-api-skeleton.

devlamine avatar devlamine commented on June 18, 2024

it works in the bootstrap "public/ index.php" it captures division error but i don't know how practical? and if i wanted to pass the connected user to Sentry how i can do that, I get It from Request ? what are the best practices

Sentry\init(['dsn' => getenv('SENTRY_DSN') ]);

try {
    $app->run();
} catch (\Throwable $exception) {
    Sentry\captureException($exception);
}

from slim4-api-skeleton.

devlamine avatar devlamine commented on June 18, 2024

Humm

it works in the bootstrap "public/ index.php" it captures division error but i don't know how practical? and if i wanted to pass the connected user to Sentry how i can do that, I get It from Request ? what are the best practices

Sentry\init(['dsn' => getenv('SENTRY_DSN') ]);

try {
    $app->run();
} catch (\Throwable $exception) {
    Sentry\captureException($exception);
}

Api errors are no longer captured, this solution does not work

from slim4-api-skeleton.

devlamine avatar devlamine commented on June 18, 2024

How I can get connected user to passe it in the scope ?

<?php

declare(strict_types=1);

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
$customErrorHandler = function (
    ServerRequestInterface $request,
    Throwable $exception,
    bool $displayErrorDetails,
    bool $logErrors,
    bool $logErrorDetails
) use ($app): Response {
    // Sentry for errors api
    Sentry\init(['dsn' => getenv('SENTRY_DSN') ]);
    
    Sentry\configureScope(function (Sentry\State\Scope $scope) use($request): void {
        $scope->setUser([
          'username' =>  'connected user',
        ]);
      });

    Sentry\captureException($exception);

    $statusCode = 500;
    if (is_int($exception->getCode()) &&
        $exception->getCode() >= 400 &&
        $exception->getCode() <= 599
    ) {
        $statusCode = $exception->getCode();
    }
    $className = new \ReflectionClass(get_class($exception));
    $data = [
        'message' => $exception->getMessage(),
        'class' => $className->getShortName(),
        'status' => 'error',
        'code' => $statusCode,
    ];
    $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    $response = $app->getResponseFactory()->createResponse();
    $response->getBody()->write($body);

    return $response
        ->withStatus($statusCode)
        ->withHeader('Content-type', 'application/problem+json');
};

return $customErrorHandler;

from slim4-api-skeleton.

maurobonfietti avatar maurobonfietti commented on June 18, 2024

Hey @devlamine , hello again!

Try something like this...

Create a new config file for Sentry App/Sentry.php:

<?php

Sentry\init(['dsn' => 'YOUR_SENTRY_DSN' ]);

// Set the Level...
Sentry\configureScope(function (Sentry\State\Scope $scope): void {
  $scope->setLevel(Sentry\Severity::warning());
});

Then require this file in the App/App.php:

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/Sentry.php';
...

Add the Sentry\captureException($exception) in App/ErrorHandler.php:

<?php

declare(strict_types=1);

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;

$customErrorHandler = function (
    ServerRequestInterface $request,
    Throwable $exception,
    bool $displayErrorDetails,
    bool $logErrors,
    bool $logErrorDetails
) use ($app): Response {
    $statusCode = 500;

    ...

    Sentry\captureException($exception);

    return $response
        ->withStatus($statusCode)
        ->withHeader('Content-type', 'application/problem+json');
};

return $customErrorHandler;

For last, do some tests like this:

final class Home
{
    public function getHelp(Request $request, Response $response): Response
    {
        // Capture a message and send it to Sentry...
        \Sentry\captureMessage('ABC');
        echo (2 / 0);
        echo intdiv(2, 0);
        \Sentry\captureMessage('ZYX');

        return JsonResponse::withJson($response, json_encode(['OK']), 200);
    }
}

In my case, I can see this in Sentry:

Screen Shot 2021-01-19 at 15 32 46


I hope this helps you.

Well, if it was useful, consider sending me coffee for my free time ☕😃.

Thanks

Good luck!!

from slim4-api-skeleton.

devlamine avatar devlamine commented on June 18, 2024

Greate job ! you are very kinde , you deserve the coffee but before you have not answered my 2nd question the user logged in in the scope, I want to retrieve it from request but it is empty :

 Sentry\configureScope(function (Sentry\State\Scope $scope) use($request): void {
        $scope->setUser([
          'username' =>  'connected user',
        ]);
      });

from slim4-api-skeleton.

maurobonfietti avatar maurobonfietti commented on June 18, 2024

Hi @devlamine !

First, you can grab the username and then configure the scope for sentry, passing the variable with the user.

For example you can try something like this:

final class Home
{
    public function getHelp(Request $request, Response $response): Response
    {
        $input = $request->getParsedBody();
        $username = $input['email'];

        \Sentry\configureScope(function (\Sentry\State\Scope $scope) use($username): void {
            $scope->setUser([
                'username' =>  $username,
            ]);
        });

        \Sentry\captureMessage('Testing Scope By User...');

        return JsonResponse::withJson($response, json_encode(['OK']), 200);
    }
}

My Sentry Dashboard Show The User Like This:

Screen Shot 2021-02-16 at 14 02 56


I'm sorry for the delay in my response.
Recently, I changed to a new job so I do not have too much free time haha. Thank you for your coffee!

from slim4-api-skeleton.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.