title |
---|
PHP Error Handler |
If your Slim Framework application throws a
PHP Runtime error (PHP 7+ only),
the application invokes its PHP Error handler and returns a
HTTP/1.1 500 Internal Server Error
response to the HTTP client.
Each Slim Framework application has a default PHP Error handler. This handler
sets the Response status to 500
, it sets the content type to text/html
,
and it writes a simple explanation to the Response body.
A Slim Framework application's PHP Error handler is a Pimple service. You can substitute your own PHP Error handler by defining a custom Pimple factory method with the application container.
{% highlight php %} // Create Slim $app = new \Slim\App(); // get the app's di-container $c = $app->getContainer(); $c['phpErrorHandler'] = function ($c) { return function ($request, $response, $error) use ($c) { return $c['response'] ->withStatus(500) ->withHeader('Content-Type', 'text/html') ->write('Something went wrong!'); }; }; {% endhighlight %}
N.B Check out Not Found docs for pre-slim creation method using a new instance of
\Slim\Container
In this example, we define a new phpErrorHandler
factory that returns a
callable. The returned callable accepts three arguments:
- A
\Psr\Http\Message\ServerRequestInterface
instance - A
\Psr\Http\Message\ResponseInterface
instance - A
\Throwable
instance
The callable MUST return a new \Psr\Http\Message\ResponseInterface
instance as is appropriate for the given error.