mirror of
https://github.com/flarum/framework.git
synced 2025-02-06 11:51:45 +08:00
Restore error details in JSON-API error formatter
Fixes #1865. Refs #1843.
This commit is contained in:
parent
a7b19284b9
commit
429b8e1a32
|
@ -26,12 +26,21 @@ class JsonApiFormatter implements HttpFormatter
|
|||
public function format(HandledError $error, Request $request): Response
|
||||
{
|
||||
$document = new Document;
|
||||
$document->setErrors([
|
||||
[
|
||||
|
||||
$data = [
|
||||
'status' => (string) $error->getStatusCode(),
|
||||
'code' => $error->getType(),
|
||||
],
|
||||
]);
|
||||
];
|
||||
$details = $error->getDetails();
|
||||
|
||||
if (empty($details)) {
|
||||
$document->setErrors([$data]);
|
||||
} else {
|
||||
$document->setErrors(array_map(
|
||||
function ($row) use ($data) { return array_merge($data, $row); },
|
||||
$details
|
||||
));
|
||||
}
|
||||
|
||||
return new JsonApiResponse($document, $error->getStatusCode());
|
||||
}
|
||||
|
|
|
@ -58,6 +58,32 @@ class CreationTest extends TestCase
|
|||
);
|
||||
|
||||
$this->assertEquals(422, $response->getStatusCode());
|
||||
|
||||
// The response body should contain details about the failed validation
|
||||
$body = (string) $response->getBody();
|
||||
$this->assertJson($body);
|
||||
$this->assertEquals([
|
||||
'errors' => [
|
||||
[
|
||||
'status' => '422',
|
||||
'code' => 'validation_error',
|
||||
'detail' => 'validation.required',
|
||||
'source' => ['pointer' => '/data/attributes/username'],
|
||||
],
|
||||
[
|
||||
'status' => '422',
|
||||
'code' => 'validation_error',
|
||||
'detail' => 'validation.required',
|
||||
'source' => ['pointer' => '/data/attributes/email'],
|
||||
],
|
||||
[
|
||||
'status' => '422',
|
||||
'code' => 'validation_error',
|
||||
'detail' => 'validation.required',
|
||||
'source' => ['pointer' => '/data/attributes/password'],
|
||||
],
|
||||
],
|
||||
], json_decode($body, true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user