Skip to content

Commit

Permalink
Restore error details in JSON-API error formatter
Browse files Browse the repository at this point in the history
Fixes #1865. Refs #1843.
  • Loading branch information
franzliedke committed Sep 3, 2019
1 parent 3eb28df commit dcf88df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Foundation/ErrorHandling/JsonApiFormatter.php
Expand Up @@ -26,12 +26,21 @@ class JsonApiFormatter implements HttpFormatter
public function format(HandledError $error, Request $request): Response
{
$document = new Document;
$document->setErrors([
[
'status' => (string) $error->getStatusCode(),
'code' => $error->getType(),
],
]);

$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());
}
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/api/users/CreationTest.php
Expand Up @@ -58,6 +58,32 @@ public function cannot_create_user_without_data()
);

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

/**
Expand Down

0 comments on commit dcf88df

Please sign in to comment.