1
0
mirror of https://github.com/flarum/core.git synced 2025-10-09 05:56:25 +02:00

Implement interface to serialize exceptions to JSON-API format

Related to #118
This commit is contained in:
Franz Liedke
2015-09-08 22:35:39 +02:00
parent c0e7ff5ea1
commit 4b4cea4d87
3 changed files with 74 additions and 2 deletions

View File

@@ -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);
}
}