Skip to content

Add Exception configuration documentation #173

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

Closed
wants to merge 0 commits into from

Conversation

GregoireHebert
Copy link
Contributor

No description provided.

@dunglas dunglas changed the base branch from master to 2.0 January 25, 2017 18:53
@dunglas dunglas changed the base branch from 2.0 to master January 25, 2017 18:53
@@ -0,0 +1,94 @@
# Exceptions
Copy link
Member

@dunglas dunglas Jan 25, 2017

Choose a reason for hiding this comment

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

I would rename this entry "Error Handling" (errors.md).

@@ -0,0 +1,94 @@
# Exceptions

API Platform Core allows you to customize the HTTP status code sent after an exception is thrown.
Copy link
Member

Choose a reason for hiding this comment

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

API Platform Core allows to customize HTTP status codes sent to clients when exceptions are thrown.


API Platform Core allows you to customize the HTTP status code sent after an exception is thrown.

## configuration
Copy link
Member

Choose a reason for hiding this comment

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

Configuration

Copy link
Member

Choose a reason for hiding this comment

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

But I'll remove this title.


## configuration

```YAML
Copy link
Member

Choose a reason for hiding this comment

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

yaml

# ...

exception_to_status:
# By default there are two exceptions defined that you can customize.
Copy link
Member

Choose a reason for hiding this comment

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

# app/config/config.yml

services:
# Map exceptions to HTTP status codes using the `exception_to_status` configuration key 
exception_to_status:
    # The 2 following exceptions are handled by default
    Symfony\Component\Serializer\Exception\ExceptionInterface: 400                     # Use a raw status code (recommended)
    ApiPlatform\Core\Exception\InvalidArgumentException: 'HTTP_BAD_REQUEST' # Or with a constant of `Symfony\Component\HttpFoundation\Response`

    AppBundle\Exception\MyException: 401 # Custom exceptions can easily be handled


As in any php application, your exceptions have to extends the \Exception class or any of it's children.

```PHP
Copy link
Member

Choose a reason for hiding this comment

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

php

*/
class CartManager
{
const NOTFOUND_DOESNOTEXISTS = 51;
Copy link
Member

Choose a reason for hiding this comment

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

I would strip the NOTFOUND prefix and a visibility to all those constants.

*
* @return Product $product
*/
public function addProductToCart($id)
Copy link
Member

Choose a reason for hiding this comment

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

public function addProductToCart(int $id)

const NOTFOUND_OUTOFSTOCK = 53;

/**
* @param integer $id
Copy link
Member

Choose a reason for hiding this comment

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

int


/*...*/

return $cart;
Copy link
Member

Choose a reason for hiding this comment

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

I would return nothing (CQRS like)

throw new ProductNotFoundException(self::NOTFOUND_OUTOFSTOCK);
}

/*...*/
Copy link
Member

Choose a reason for hiding this comment

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

Can be removed

@dunglas
Copy link
Member

dunglas commented Jan 25, 2017

I would merge both sections (the last section should be an example illustrating the config). I propose to start with the tiny exception example (it can simplified IMO, and it should be more integrated with API Platform, for instance used as an event listener).

@dunglas
Copy link
Member

dunglas commented Jan 25, 2017

By the way it would be nice to show that API Platform automatically handle validation errors (it can be another section)

}
```

It doesn't have to be an HttpException, any type of Exception can be thrown. The best part is that API Platform already takes care of how the error is handled and returned. If you configured your API to respond in a JsonLD format, your error will be returned in JsonLD format as well.
Copy link
Member

Choose a reason for hiding this comment

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

The exception doesn't have to be a Symfony's `HttpException`. Any type of `Exception` can be thrown. The best part is that API Platform already takes care of how the error is handled and returned. For instance the API is configured to respond in JSON-LD, the error will be returned in this format as well.

@GregoireHebert
Copy link
Contributor Author

GregoireHebert commented Jan 25, 2017

I'll do the example for validation errors and transform the exception example as an event listener tomorrow.

@GregoireHebert
Copy link
Contributor Author

GregoireHebert commented Jan 31, 2017

Ok I made some changes, but I am not really convinced that it is what you had in mind.
Let me know :)

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.

2 participants