Error handling with Sentry

Hello, I’m having trouble to understand how error handling works in Slim. I understood that if you didn’t set an error middleware then uncaught exceptions would go to whatever error handler you had installed.

But it seems that is not the case, as I can see some uncaught exceptions in the logs that are not sent to Sentry, which is set to catch all unhandled exceptions.

So, can you confirm me please that if you don’t add an error middleware then the exceptions are logged but then swallowed by Slim?

And if so, what would be the intended behaviour, to always define the error middleware? To rethrow the exception from there?

In Slim 4, error handling is indeed managed by the ErrorMiddleware. If you don’t add the Slim ErrorMiddleware, and there’s no other custom error handling mechanism in place, then errors will fall back to PHP’s native error handling. This means that PHP will handle the error according to its configuration, which is set in the php.ini file.

If your php.ini is configured to log errors (e.g., log_errors = On and you’ve specified an error_log file), then unhandled exceptions will be logged to that file.

So, if you’re seeing uncaught exceptions in some logs and you haven’t added Slim’s ErrorMiddleware, it’s likely that these logs are coming from PHP’s native error handler.

If you want exceptions to be handled by a specific tool (like Sentry), you should either:

  • Integrate Sentry within Slim by adding the ErrorMiddleware and configuring it appropriately, as I showed in the following example:
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

$errorMiddleware->setDefaultErrorHandler(function ($request, $exception, $displayErrorDetails, $logErrors, $logErrorDetails) use ($app) {
    // Send exception to Sentry
    Sentry\captureException($exception);

    // You can return a custom error response if needed
    $response = $app->getResponseFactory()->createResponse();
    $response->getBody()->write("An error occurred.");
    return $response;
});
  • Or, configure Sentry at the global PHP level, outside of Slim, to catch all exceptions and errors. This can be done by setting up Sentry as an error and exception handler in PHP. This is a broader approach and will catch exceptions regardless of whether they’re thrown within Slim or in other parts of your code.

Here’s an example of how it can look:

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

// Initialize Sentry
Sentry\init(['dsn' => 'YOUR_DSN_HERE']);

// Your Slim application initialization and other code follows
$app = Slim\Factory\AppFactory::create();

// ... rest of your application ...
$app->run();

With the above setup, Sentry will catch any unhandled exceptions or errors, whether they are from Slim or any other part of your PHP application.

If you also set up an error handler within Slim that handles exceptions (like the ErrorMiddleware), you should decide where you want the exception to be logged. If you handle and log it within Slim, you might prevent it from reaching the global Sentry handler, unless you rethrow the exception.

A good practice is to decide on a single, consistent way to handle exceptions, whether that’s within Slim or at the global PHP level, to avoid duplicate logs or missing any exceptions.

Hi @odan,

Thanks for the thorough response. Yes, that’s exactly what I understood from the docs (well, it wasn’t totally clear to me what happened if you didn’t set the error middleware).

The thing is that Sentry was behaving very strangely and at some point I thought it was because the error middleware in Slim, but then I realised that even some errors outside of Slim were not sent to Sentry.

It was a nonsense behaviour, with errors skipped with total randomness. So what I did is to update Sentry to the latest version (from 3.2 to 3.5) and so far (will need a few days to tell for sure) no errors seem skipped.

Again, thanks for the quick reply.

1 Like

I am using Slim framework and I want to handle 500 internal server error (actually I want to send a email when I got 500 error) so can you please provide a solution for this.
I am using this but not able to handle with this :
$container[‘phpErrorHandler’] = function ($container) {}
$c[‘errorHandler’] = function ($c) {}

Hi @Ganesh Please create a new topic and show us more details.