1
0
mirror of https://github.com/flarum/core.git synced 2025-08-01 22:20:21 +02:00

Improve API error handling

- Change 'path' key to 'source.pointer', as per spec
- Add 500 error detail if debug mode is on
This commit is contained in:
Toby Zerner
2015-09-14 15:40:07 +09:30
parent b7d8afe6a4
commit 8cccaaaf6b
5 changed files with 54 additions and 27 deletions

View File

@@ -12,6 +12,21 @@ namespace Flarum\Core\Exceptions;
use Exception;
class InvalidConfirmationTokenException extends Exception
class InvalidConfirmationTokenException extends Exception implements JsonApiSerializable
{
/**
* {@inheritdoc}
*/
public function getStatusCode()
{
return 403;
}
/**
* {@inheritdoc}
*/
public function getErrors()
{
return ['code' => 'invalid_confirmation_token'];
}
}

View File

@@ -27,9 +27,7 @@ class ValidationException extends Exception implements JsonApiSerializable
}
/**
* Return the HTTP status code to be used for this exception.
*
* @return int
* {@inheritdoc}
*/
public function getStatusCode()
{
@@ -37,15 +35,14 @@ class ValidationException extends Exception implements JsonApiSerializable
}
/**
* Return an array of errors, formatted as JSON-API error objects.
*
* @see http://jsonapi.org/format/#error-objects
* @return array
* {@inheritdoc}
*/
public function getErrors()
{
return array_map(function ($path, $detail) {
return compact('path', 'detail');
$source = ['pointer' => '/data/attributes/' . $path];
return compact('source', 'detail');
}, array_keys($this->messages), $this->messages);
}
}