mirror of
https://github.com/flarum/framework.git
synced 2025-02-24 01:20:55 +08:00
Distinguish between attributes/relationships in ValidationException
This exception could be a candidate for inclusion in tobscure/json-api...
This commit is contained in:
parent
1d0621951b
commit
bdc0bd3d02
@ -30,18 +30,23 @@ class ValidationExceptionHandler implements ExceptionHandlerInterface
|
|||||||
*/
|
*/
|
||||||
public function handle(Exception $e)
|
public function handle(Exception $e)
|
||||||
{
|
{
|
||||||
$status = 422;
|
$errors = array_merge(
|
||||||
|
$this->buildErrors($e->getAttributes(), '/data/attributes'),
|
||||||
|
$this->buildErrors($e->getRelationships(), '/data/relationships')
|
||||||
|
);
|
||||||
|
|
||||||
$messages = $e->getMessages();
|
return new ResponseBag(422, $errors);
|
||||||
$errors = array_map(function ($path, $detail) use ($status) {
|
}
|
||||||
|
|
||||||
|
private function buildErrors(array $messages, $pointer)
|
||||||
|
{
|
||||||
|
return array_map(function ($path, $detail) use ($pointer) {
|
||||||
return [
|
return [
|
||||||
'status' => (string) $status,
|
'status' => '422',
|
||||||
'code' => 'validation_error',
|
'code' => 'validation_error',
|
||||||
'detail' => $detail,
|
'detail' => $detail,
|
||||||
'source' => ['pointer' => "/data/attributes/$path"]
|
'source' => ['pointer' => $pointer.'/'.$path]
|
||||||
];
|
];
|
||||||
}, array_keys($messages), $messages);
|
}, array_keys($messages), $messages);
|
||||||
|
|
||||||
return new ResponseBag($status, $errors);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,17 +14,26 @@ use Exception;
|
|||||||
|
|
||||||
class ValidationException extends Exception
|
class ValidationException extends Exception
|
||||||
{
|
{
|
||||||
protected $messages;
|
protected $attributes;
|
||||||
|
protected $relationships;
|
||||||
|
|
||||||
public function __construct(array $messages)
|
public function __construct(array $attributes, array $relationships = [])
|
||||||
{
|
{
|
||||||
$this->messages = $messages;
|
$this->attributes = $attributes;
|
||||||
|
$this->relationships = $relationships;
|
||||||
|
|
||||||
|
$messages = [implode("\n", $attributes), implode("\n", $relationships)];
|
||||||
|
|
||||||
parent::__construct(implode("\n", $messages));
|
parent::__construct(implode("\n", $messages));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMessages()
|
public function getAttributes()
|
||||||
{
|
{
|
||||||
return $this->messages;
|
return $this->attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRelationships()
|
||||||
|
{
|
||||||
|
return $this->relationships;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,15 +32,24 @@ class ValidationExceptionHandlerTest extends TestCase
|
|||||||
|
|
||||||
public function test_managing_exceptions()
|
public function test_managing_exceptions()
|
||||||
{
|
{
|
||||||
$response = $this->handler->handle(new ValidationException(['There was an error']));
|
$response = $this->handler->handle(new ValidationException(
|
||||||
|
['foo' => 'Attribute error'],
|
||||||
|
['bar' => 'Relationship error']
|
||||||
|
));
|
||||||
|
|
||||||
$this->assertEquals(422, $response->getStatus());
|
$this->assertEquals(422, $response->getStatus());
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
[
|
[
|
||||||
'status' => '422',
|
'status' => '422',
|
||||||
'code' => 'validation_error',
|
'code' => 'validation_error',
|
||||||
'detail' => 'There was an error',
|
'detail' => 'Attribute error',
|
||||||
'source' => ['pointer' => '/data/attributes/0']
|
'source' => ['pointer' => '/data/attributes/foo']
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'status' => '422',
|
||||||
|
'code' => 'validation_error',
|
||||||
|
'detail' => 'Relationship error',
|
||||||
|
'source' => ['pointer' => '/data/relationships/bar']
|
||||||
]
|
]
|
||||||
], $response->getErrors());
|
], $response->getErrors());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user