-
-
Notifications
You must be signed in to change notification settings - Fork 933
Add support for the API Error format (RFC 7807) and more tests #627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Feature: Error handling valid according to RFC 7807 (application/problem+json) | ||
In order to be able to handle error client side | ||
As a client software developer | ||
I need to retrieve an RFC 7807 compliant serialization of errors | ||
|
||
Scenario: Get an error | ||
When I add "Accept" header equal to "application/json" | ||
And I send a "POST" request to "/dummies" with body: | ||
""" | ||
{} | ||
""" | ||
Then the response status code should be 400 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/problem+json" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"type": "https://tools.ietf.org/html/rfc2616#section-10", | ||
"title": "An error occurred", | ||
"detail": "name: This value should not be blank.", | ||
"violations": [ | ||
{ | ||
"propertyPath": "name", | ||
"message": "This value should not be blank." | ||
} | ||
] | ||
} | ||
""" | ||
|
||
Scenario: Get an error during deserialization of simple relation | ||
When I add "Accept" header equal to "application/json" | ||
And I send a "POST" request to "/dummies" with body: | ||
""" | ||
{ | ||
"name": "Foo", | ||
"relatedDummy": { | ||
"name": "bar" | ||
} | ||
} | ||
""" | ||
Then the response status code should be 400 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/problem+json" | ||
And the JSON node "type" should be equal to "https://tools.ietf.org/html/rfc2616#section-10" | ||
And the JSON node "title" should be equal to "An error occurred" | ||
And the JSON node "detail" should be equal to 'Nested objects for attribute "relatedDummy" of "ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy" are not enabled. Use serialization groups to change that behavior.' | ||
And the JSON node "trace" should exist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Action; | ||
|
||
use ApiPlatform\Core\Exception\InvalidArgumentException; | ||
use ApiPlatform\Core\Util\ErrorFormatGuesser; | ||
use Symfony\Component\Debug\Exception\FlattenException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Serializer\Exception\ExceptionInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* Renders a normalized exception for a given {@see \Symfony\Component\Debug\Exception\FlattenException}. | ||
* | ||
* @author Baptiste Meyer <baptiste.meyer@gmail.com> | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
final class ExceptionAction | ||
{ | ||
const DEFAULT_EXCEPTION_TO_STATUS = [ | ||
ExceptionInterface::class => Response::HTTP_BAD_REQUEST, | ||
InvalidArgumentException::class => Response::HTTP_BAD_REQUEST, | ||
]; | ||
|
||
private $serializer; | ||
private $errorFormats; | ||
private $exceptionToStatus; | ||
|
||
public function __construct(SerializerInterface $serializer, array $errorFormats, $exceptionToStatus = []) | ||
{ | ||
$this->serializer = $serializer; | ||
$this->errorFormats = $errorFormats; | ||
$this->exceptionToStatus = array_merge(self::DEFAULT_EXCEPTION_TO_STATUS, $exceptionToStatus); | ||
} | ||
|
||
/** | ||
* Converts a an exception to a JSON response. | ||
* | ||
* @param \Exception|FlattenException $exception | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function __invoke($exception, Request $request) : Response | ||
{ | ||
$exceptionClass = $exception->getClass(); | ||
foreach ($this->exceptionToStatus as $class => $status) { | ||
if (is_a($exceptionClass, $class, true)) { | ||
$exception->setStatusCode($status); | ||
|
||
break; | ||
} | ||
} | ||
|
||
$headers = $exception->getHeaders(); | ||
$format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats); | ||
$headers['Content-Type'] = $format['value'][0]; | ||
|
||
return new Response($this->serializer->serialize($exception, $format['key']), $exception->getStatusCode(), $headers); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC 2616 is obsolete. See RFC 7231 instead.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this should be (if we use specific URIs for each status code):
p/s: The indentation in this section is wrong (3 spaces instead of the intended 4)