-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
core/exceptions.md
Outdated
@@ -0,0 +1,94 @@ | |||
# Exceptions |
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.
I would rename this entry "Error Handling" (errors.md
).
core/exceptions.md
Outdated
@@ -0,0 +1,94 @@ | |||
# Exceptions | |||
|
|||
API Platform Core allows you to customize the HTTP status code sent after an exception is thrown. |
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.
API Platform Core allows to customize HTTP status codes sent to clients when exceptions are thrown.
core/exceptions.md
Outdated
|
||
API Platform Core allows you to customize the HTTP status code sent after an exception is thrown. | ||
|
||
## configuration |
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.
Configuration
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.
But I'll remove this title.
core/exceptions.md
Outdated
|
||
## configuration | ||
|
||
```YAML |
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.
yaml
core/exceptions.md
Outdated
# ... | ||
|
||
exception_to_status: | ||
# By default there are two exceptions defined that you can customize. |
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.
# 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
core/exceptions.md
Outdated
|
||
As in any php application, your exceptions have to extends the \Exception class or any of it's children. | ||
|
||
```PHP |
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.
php
core/exceptions.md
Outdated
*/ | ||
class CartManager | ||
{ | ||
const NOTFOUND_DOESNOTEXISTS = 51; |
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.
I would strip the NOTFOUND
prefix and a visibility to all those constants.
core/exceptions.md
Outdated
* | ||
* @return Product $product | ||
*/ | ||
public function addProductToCart($id) |
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.
public function addProductToCart(int $id)
core/exceptions.md
Outdated
const NOTFOUND_OUTOFSTOCK = 53; | ||
|
||
/** | ||
* @param integer $id |
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.
int
core/exceptions.md
Outdated
|
||
/*...*/ | ||
|
||
return $cart; |
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.
I would return nothing (CQRS like)
core/exceptions.md
Outdated
throw new ProductNotFoundException(self::NOTFOUND_OUTOFSTOCK); | ||
} | ||
|
||
/*...*/ |
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.
Can be removed
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). |
By the way it would be nice to show that API Platform automatically handle validation errors (it can be another section) |
core/exceptions.md
Outdated
} | ||
``` | ||
|
||
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. |
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.
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.
I'll do the example for validation errors and transform the exception example as an event listener tomorrow. |
Ok I made some changes, but I am not really convinced that it is what you had in mind. |
No description provided.