Comments (8)
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:
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.
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.
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.
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.
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.
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:
I hope this helps you.
Well, if it was useful, consider sending me coffee for my free time ☕😃.
Good luck!!
from slim4-api-skeleton.
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.
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:
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)
- Integrate Phinx or other migration database system HOT 2
- slim4-api-skeleton as docker container without mysql in container HOT 2
- composer test not working HOT 6
- Missing middleware skeleton HOT 2
- docker-compose without mysql HOT 1
- How to handle with exceptions? HOT 2
- composer test no arranca, Tests: 4/4 HOT 2
- About running the docker compose HOT 2
- Issue in tests HOT 1
- Adding monolog support? HOT 2
- Role based authorization HOT 8
- Add an option for setBasePath HOT 1
- Abandoned Packages Updated HOT 1
- Undefined property: stdClass:: HOT 4
- automatic generation of a Swagger / OpenAPI JSON file
- Error , could not find driver HOT 1
- Incorrect path to php service HOT 1
- require_once change in composer.json
- upgrade to php 8.3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slim4-api-skeleton.