Implement middleware for handling errors according to JSON API spec

This commit is contained in:
Franz Liedke 2015-06-20 19:45:32 +02:00
parent e8bd58ec07
commit 626daa9f38

View File

@ -0,0 +1,36 @@
<?php namespace Flarum\Api\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\StringResponse;
use Zend\Stratigility\ErrorMiddlewareInterface;
class JsonApiErrors implements ErrorMiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function __invoke($error, Request $request, Response $response, callable $out = null)
{
$errorObject = [
'title' => $error->getMessage(),
];
$status = 500;
// If it seems to be a valid HTTP status code, we pass on the
// exception's status.
$errorCode = $error->getCode();
if (is_int($errorCode) && $errorCode >= 400 && $errorCode < 600) {
$status = $errorCode;
}
// JSON API errors must be collected in an array under the
// "errors" key in the top level of the document
$data = [
'errors' => [$errorObject]
];
return StringResponse::json($data, $status);
}
}