System Error Handler

Things go wrong. You can’t predict errors, but you can anticipate them. Each Slim Framework application has an error handler that receives all uncaught PHP exceptions. This error handler also receives the current HTTP request and response objects, too. The error handler must prepare and return an appropriate Response object to be returned to the HTTP client.

Default error handler

The default error handler is very basic. It sets the Response status code to 500, it sets the Response content type to text/html, and appends a generic error message into the Response body.

This is probably not appropriate for production applications. You are strongly encouraged to implement your own Slim application error handler.

The default error handler can also include detailed error diagnostic information. To enable this you need to set the displayErrorDetails setting to true:

  1. $configuration = [
  2. 'settings' => [
  3. 'displayErrorDetails' => true,
  4. ],
  5. ];
  6. $c = new \Slim\Container($configuration);
  7. $app = new \Slim\App($c);

Custom error handler

A Slim Framework application’s error handler is a Pimple service. You can substitute your own error handler by defining a custom Pimple factory method with the application container.

There are two ways to inject handlers:

Pre App

  1. $c = new \Slim\Container();
  2. $c['errorHandler'] = function ($c) {
  3. return function ($request, $response, $exception) use ($c) {
  4. return $response->withStatus(500)
  5. ->withHeader('Content-Type', 'text/html')
  6. ->write('Something went wrong!');
  7. };
  8. };
  9. $app = new \Slim\App($c);

Post App

  1. $app = new \Slim\App();
  2. $c = $app->getContainer();
  3. $c['errorHandler'] = function ($c) {
  4. return function ($request, $response, $exception) use ($c) {
  5. return $response->withStatus(500)
  6. ->withHeader('Content-Type', 'text/html')
  7. ->write('Something went wrong!');
  8. };
  9. };

In this example, we define a new errorHandler factory that returns a callable. The returned callable accepts three arguments:

  • A \Psr\Http\Message\ServerRequestInterface instance
  • A \Psr\Http\Message\ResponseInterface instance
  • A \Exception instanceThe callable MUST return a new \Psr\Http\Message\ResponseInterface instance as is appropriate for the given exception.

Class-based error handler

Error handlers may also be defined as an invokable class.

  1. class CustomHandler {
  2. public function __invoke($request, $response, $exception) {
  3. return $response
  4. ->withStatus(500)
  5. ->withHeader('Content-Type', 'text/html')
  6. ->write('Something went wrong!');
  7. }
  8. }

and attached like so:

  1. $app = new \Slim\App();
  2. $c = $app->getContainer();
  3. $c['errorHandler'] = function ($c) {
  4. return new CustomHandler();
  5. };

This allows us to define more sophisticated handlers or extend/override thebuilt-in Slim\Handlers* classes.

Handling other errors

Please note: The following four types of exceptions will not be handled by a custom errorHandler:

  • Slim\Exception\MethodNotAllowedException: This can be handled via a custom notAllowedHandler.
  • Slim\Exception\NotFoundException: This can be handled via a custom notFoundHandler.
  • Runtime PHP errors (PHP 7+ only): This can be handled via a custom phpErrorHandler.
  • Slim\Exception\SlimException: This type of exception is internal to Slim, and its handling cannot be overridden.

Disabling

To completely disable Slim’s error handling, simply remove the error handler from the container:

  1. unset($app->getContainer()['errorHandler']);
  2. unset($app->getContainer()['phpErrorHandler']);

You are now responsible for handling any exceptions that occur in your application as they will not be handled by Slim.