From 774505299cb629f862b9d3af62103035a9f3caa8 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 8 Sep 2015 22:35:39 +0200 Subject: [PATCH] Implement interface to serialize exceptions to JSON-API format Related to #118 --- .../Core/Exceptions/JsonApiSerializable.php | 29 +++++++++++++++++++ .../Exceptions/PermissionDeniedException.php | 22 +++++++++++++- .../Core/Exceptions/ValidationException.php | 25 +++++++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 framework/core/src/Core/Exceptions/JsonApiSerializable.php diff --git a/framework/core/src/Core/Exceptions/JsonApiSerializable.php b/framework/core/src/Core/Exceptions/JsonApiSerializable.php new file mode 100644 index 000000000..4ff671d22 --- /dev/null +++ b/framework/core/src/Core/Exceptions/JsonApiSerializable.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Core\Exceptions; + +interface JsonApiSerializable +{ + /** + * Return the HTTP status code to be used for this exception. + * + * @return int + */ + public function getStatusCode(); + + /** + * Return an array of errors, formatted as JSON-API error objects. + * + * @see http://jsonapi.org/format/#error-objects + * @return array + */ + public function getErrors(); +} diff --git a/framework/core/src/Core/Exceptions/PermissionDeniedException.php b/framework/core/src/Core/Exceptions/PermissionDeniedException.php index 14b7c1d66..9cffa8eb3 100644 --- a/framework/core/src/Core/Exceptions/PermissionDeniedException.php +++ b/framework/core/src/Core/Exceptions/PermissionDeniedException.php @@ -12,6 +12,26 @@ namespace Flarum\Core\Exceptions; use Exception; -class PermissionDeniedException extends Exception +class PermissionDeniedException extends Exception implements JsonApiSerializable { + /** + * Return the HTTP status code to be used for this exception. + * + * @return int + */ + public function getStatusCode() + { + return 401; + } + + /** + * Return an array of errors, formatted as JSON-API error objects. + * + * @see http://jsonapi.org/format/#error-objects + * @return array + */ + public function getErrors() + { + return []; + } } diff --git a/framework/core/src/Core/Exceptions/ValidationException.php b/framework/core/src/Core/Exceptions/ValidationException.php index 56ff781a9..2de94c316 100644 --- a/framework/core/src/Core/Exceptions/ValidationException.php +++ b/framework/core/src/Core/Exceptions/ValidationException.php @@ -12,7 +12,7 @@ namespace Flarum\Core\Exceptions; use Exception; -class ValidationException extends Exception +class ValidationException extends Exception implements JsonApiSerializable { protected $messages; @@ -25,4 +25,27 @@ class ValidationException extends Exception { return $this->messages; } + + /** + * Return the HTTP status code to be used for this exception. + * + * @return int + */ + public function getStatusCode() + { + return 422; + } + + /** + * Return an array of errors, formatted as JSON-API error objects. + * + * @see http://jsonapi.org/format/#error-objects + * @return array + */ + public function getErrors() + { + return array_map(function ($path, $detail) { + return compact('path', 'detail'); + }, array_keys($this->messages), $this->messages); + } }