1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 07:24:27 +02:00

Move exception handling for Flarum exception classes to middleware

Related to #118.
This commit is contained in:
Franz Liedke
2015-09-08 22:36:32 +02:00
parent 4b4cea4d87
commit b8ac49ffcc
2 changed files with 17 additions and 20 deletions

View File

@@ -10,6 +10,7 @@
namespace Flarum\Api\Middleware;
use Flarum\Core\Exceptions\JsonApiSerializable;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
@@ -22,23 +23,29 @@ class JsonApiErrors implements ErrorMiddlewareInterface
*/
public function __invoke($error, Request $request, Response $response, callable $out = null)
{
$errorObject = [
'title' => $error->getMessage(),
];
if ($error instanceof JsonApiSerializable) {
$status = $error->getStatusCode();
$status = 500;
$errors = $error->getErrors();
} else {
$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;
$errors = [
['title' => $error->getMessage()]
];
// 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]
'errors' => $errors,
];
return new JsonResponse($data, $status);