maurobonfietti / slim4-api-skeleton

Useful skeleton for RESTful API development with PHP and Slim 4.
http://bit.ly/2nNNOZi
MIT License
132 stars 32 forks source link

Slim 4 api with sentry #50

Closed devlamine closed 3 years ago

devlamine commented 3 years ago

Hello there, can you help me to integrate your greate api with Sentry? Thank you

maurobonfietti commented 3 years ago

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. 😄

devlamine commented 3 years ago

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);
        }
    }
}
devlamine commented 3 years ago

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);
}
devlamine commented 3 years ago

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

devlamine commented 3 years ago

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;
maurobonfietti commented 3 years ago

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!!

devlamine commented 3 years ago

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',
        ]);
      });
maurobonfietti commented 3 years ago

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!