I want to override the default error handlers in Slim 3 and respond with JSON instead of the default HTML page. But I can't get it to work, my custom handlers are ignored completely and I can't figure out why.
My project structure looks like this:
api/
public/
index.php
src/
config/
database.php
handlers.php
settings.php
models/
product.php
routes/
products.php
My index.php
looks like this:
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../src/config/database.php';
// Instantiate the app
$settings = require __DIR__ . '/../src/config/settings.php';
$app = new \Slim\App(['settings' => $settings]);
// Set up handlers
$container = $app->getContainer();
require __DIR__ . "/../src/config/handlers.php";
// Register routes
require __DIR__ . '/../src/routes/products.php';
// Run app
$app->run();
And my handlers.php
file with all the custom error handlers looks like this:
<?php
/**
* Custom global error handler.
*/
$container['errorHandler'] = function($container) {
return function ($request, $response, $exception) use ($container) {
return $response->withStatus(500)
->withHeader('Content-Type', 'application/json')
->write(json_encode(array(
'error' => 'INTERNAL_ERROR',
'error_message' => 'Something went wrong internally.',
'status_code' => '500',
'trace' => $exception.getTraceAsString()
), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
};
};
/**
* Custom global PHP error handler.
*/
$container['phpErrorHandler'] = function($container) {
return $container['errorHandler'];
};
/**
* Custom 404 Not Found error handler.
*/
$container['notFoundHandler'] = function($container) {
return function ($request, $response) use ($container) {
return $response->withStatus(404)
->withHeader('Content-Type', 'application/json')
->write(json_encode(array(
'error' => 'NOT_FOUND',
'error_message' => 'Endpoint was not found. Check API documentation for valid endpoints.',
'status_code' => '404',
), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
};
};
/**
* Custom 405 Not Allowed error handler.
*/
$container['notAllowedHandler'] = function($container) {
return function ($request, $response, $methods) use ($container) {
return $response->withStatus(405)
->withHeader('Allow', implode(', ', $methods))
->withHeader('Content-Type', 'application/json')
->write(json_encode(array(
'error' => 'NOT_ALLOWED',
'error_message' => 'HTTP request method is not allowed. Method must be of: ' . implode(', ', $methods),
'status_code' => '405',
), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
};
};
What I have tried so far:
- Adding all handlers before creating
$app
and loading the$container
into it. - Unset the existing handler before creating a custom one, like so:
unset($app->getContainer()['notFoundHandler']);
I just can't wrap my head around what goes wrong and why I am still getting the default HTML views when an error is thrown.