mirror of
https://github.com/flarum/core.git
synced 2025-08-01 22:20:21 +02:00
Implement interface to serialize exceptions to JSON-API format
Related to #118
This commit is contained in:
29
framework/core/src/Core/Exceptions/JsonApiSerializable.php
Normal file
29
framework/core/src/Core/Exceptions/JsonApiSerializable.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* 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();
|
||||||
|
}
|
@@ -12,6 +12,26 @@ namespace Flarum\Core\Exceptions;
|
|||||||
|
|
||||||
use Exception;
|
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 [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ namespace Flarum\Core\Exceptions;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
class ValidationException extends Exception
|
class ValidationException extends Exception implements JsonApiSerializable
|
||||||
{
|
{
|
||||||
protected $messages;
|
protected $messages;
|
||||||
|
|
||||||
@@ -25,4 +25,27 @@ class ValidationException extends Exception
|
|||||||
{
|
{
|
||||||
return $this->messages;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user