Skip to content

Add the ability to configure and match exceptions with an HTTP status code#797

Merged
dunglas merged 1 commit into
api-platform:masterfrom
meyerbaptiste:add_exception_to_status_configuration
Oct 14, 2016
Merged

Add the ability to configure and match exceptions with an HTTP status code#797
dunglas merged 1 commit into
api-platform:masterfrom
meyerbaptiste:add_exception_to_status_configuration

Conversation

@meyerbaptiste

@meyerbaptiste meyerbaptiste commented Oct 11, 2016

Copy link
Copy Markdown
Member
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #405
License MIT
Doc PR N/A

This PR adds the ability to configure and match exceptions with an HTTP status code and to override default exceptions (i.e., Symfony\Component\Serializer\Exception\UnexpectedValueException and Symfony\Component\Serializer\Exception\ExceptionInterface to 400 HTTP error).

Example of configuration:

api_platform:
  exception_to_status:
    Foo\Bar\BazException: 400
    Baz\Bar\FooException: "HTTP_BAD_REQUEST" # or use a constant defined in the 'Symfony\Component\HttpFoundation\Response' class

@meyerbaptiste
meyerbaptiste force-pushed the add_exception_to_status_configuration branch 2 times, most recently from 7df3cb2 to c5648d8 Compare October 11, 2016 13:46

@theofidry theofidry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the idea 👍

private $exceptionToStatus;

public function __construct(SerializerInterface $serializer, array $errorFormats, $exceptionToStatus = [])
public function __construct(SerializerInterface $serializer, array $errorFormats, array $exceptionToStatus = [])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a phpdoc with examples of the possible values for $errorFormats and $exceptionToStatus, it is not clear from just looking at the constructor what those values can be although the tests gives some hints

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done for all @theofidry

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessageRegExp /The HTTP status code ".+" is not valid\./
* @dataProvider invalidHttpStatusCodeProvider

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would declare the @dataProvider first, to keep the lexicographical order

enable_nelmio_api_doc: true
exception_to_status:
Symfony\Component\Serializer\Exception\ExceptionInterface: 400
ApiPlatform\Core\Exception\InvalidArgumentException: 'HTTP_BAD_REQUEST'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 really like the improvement, this however should be documented!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will open a PR for the doc.

@meyerbaptiste
meyerbaptiste force-pushed the add_exception_to_status_configuration branch 2 times, most recently from e22295b to 58b6a24 Compare October 13, 2016 15:58
@meyerbaptiste
meyerbaptiste force-pushed the add_exception_to_status_configuration branch from 58b6a24 to e9c1863 Compare October 13, 2016 15:59
@dunglas
dunglas merged commit 38195c2 into api-platform:master Oct 14, 2016
@dunglas

dunglas commented Oct 14, 2016

Copy link
Copy Markdown
Member

Thanks @meyerbaptiste

soyuka pushed a commit to soyuka/doc that referenced this pull request Oct 14, 2016
soyuka pushed a commit to soyuka/doc that referenced this pull request Oct 14, 2016
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
…to_status_configuration

Add the ability to configure and match exceptions with an HTTP status code
@meyerbaptiste
meyerbaptiste deleted the add_exception_to_status_configuration branch April 25, 2017 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants