diff --git a/framework/core/src/Api/Middleware/JsonApiErrors.php b/framework/core/src/Api/Middleware/JsonApiErrors.php new file mode 100644 index 000000000..8345b878e --- /dev/null +++ b/framework/core/src/Api/Middleware/JsonApiErrors.php @@ -0,0 +1,36 @@ + $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); + } +}