I have a curious problem where Slim Framework v4 triggers the error handler twice. Once for an exception, once for a PHP undefined index
error. I can and will fix the index error, but in development this results in invalid JSON response.
The project is set up using the Skeleton project and the error handler is the original one found in the repo.
Here is the response:
{
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "Missing foo"
}
}{
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "ERROR: Undefined index: foo on line 289 in file /var/www/src/Modules/Internal/Controllers/InternalController.php."
}
}
The example response below is from a catched exception, which gets returned as a 401 response:
{
"error": "Missing parameter foo"
}{
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "ERROR: Undefined index: foo on line 291 in file /var/www/src/Modules/Internal/Controllers/InternalController.php."
}
}
The errors can be triggered by a line like this, which is of course an Undefined index
error:
if (!isset($body['foo']) && !is_string($body['foo']))
The fix would be to change AND to OR:
if (!isset($body['foo']) || !is_string($body['foo']))
My question is how can I get access to an existing response in the HttpErrorHandler
, to pool these responses to an array, like this:
{
"errors": [
{ "error": "Missing parameter foo" },
{ "statusCode": 500... }
}
}
I’ve tried digging through the Request object and anything I can, but I’d like a tip.