Skip to content

Commit 71a09e7

Browse files
Add Exception configuration documentation
1 parent 257b4ba commit 71a09e7

File tree

4 files changed

+103
-8
lines changed

4 files changed

+103
-8
lines changed

core/events.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ Constant | Event | Priority |
106106
`PRE_RESPOND` | `kernel.view` | 9 |
107107
`POST_RESPOND` | `kernel.response` | 0 |
108108

109-
Previous chapter: [Pagination](pagination.md)
109+
Previous chapter: [Exceptions](exceptions.md)
110110

111111
Next chapter: [Content Negotiation](content-negotiation.md)

core/exceptions.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Exceptions
2+
3+
API Platform Core allows you to customize the HTTP status code sent after an exception is thrown.
4+
5+
## configuration
6+
7+
```YAML
8+
# app/config/config.yml
9+
10+
api_platform:
11+
12+
# ...
13+
14+
exception_to_status:
15+
# By default there are two exceptions defined that you can customize.
16+
17+
# With a status code (recommended).
18+
Symfony\Component\Serializer\Exception\ExceptionInterface: 400
19+
20+
# Or with a constant defined in the 'Symfony\Component\HttpFoundation\Response' class.
21+
ApiPlatform\Core\Exception\InvalidArgumentException: 'HTTP_BAD_REQUEST'
22+
23+
# You can then add yours
24+
Foo\Bar\BazException: 401
25+
26+
# ...
27+
```
28+
29+
## Custom Exception
30+
31+
As in any php application, your exceptions have to extends the \Exception class or any of it's children.
32+
33+
```PHP
34+
<?php
35+
36+
namespace AppBundle\Exception;
37+
38+
/**
39+
* Class ProductNotFoundException
40+
*/
41+
class ProductNotFoundException extends \Exception
42+
{
43+
44+
public function __construct($code = 0) {
45+
parent::__construct("Product Not found", $code);
46+
}
47+
}
48+
```
49+
50+
```PHP
51+
<?php
52+
53+
namespace AppBundle\Manager;
54+
55+
use AppBundle\Entity\Product;
56+
use AppBundle\Exception\ProductNotFoundException;
57+
58+
/**
59+
* Class CartManager
60+
*/
61+
class CartManager
62+
{
63+
const NOTFOUND_DOESNOTEXISTS = 51;
64+
const NOTFOUND_DEACTIVATED = 52;
65+
const NOTFOUND_OUTOFSTOCK = 53;
66+
67+
/**
68+
* @param integer $id
69+
*
70+
* @throws ProductNotFoundException
71+
*
72+
* @return Product $product
73+
*/
74+
public function addProductToCart($id)
75+
{
76+
$product = /*...*/;
77+
78+
if (!$product->getVirtualStock()) {
79+
// Using internal codes for a better understanding of what's going on
80+
throw new ProductNotFoundException(self::NOTFOUND_OUTOFSTOCK);
81+
}
82+
83+
/*...*/
84+
85+
return $cart;
86+
}
87+
}
88+
```
89+
90+
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.
91+
92+
Previous chapter: [Pagination](pagination.md)
93+
94+
Next chapter: [The Event System](events.md)

core/pagination.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,4 @@ class Book
208208

209209
Previous chapter: [Validation](validation.md)
210210

211-
Next chapter: [The Event System](events.md)
211+
Next chapter: [Exceptions](exceptions.md)

index.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@
4747
8. [Pagination](core/pagination.md)
4848
1. [Disabling the Pagination](core/pagination.md#disabling-the-pagination)
4949
2. [Changing the Number of Items per Page](core/pagination.md#changing-the-number-of-items-per-page)
50-
9. [The Event System](core/events.md)
51-
10. [Content Negotiation](core/content-negotiation.md)
50+
9. [Exceptions](core/exceptions.md)
51+
10. [The Event System](core/events.md)
52+
11. [Content Negotiation](core/content-negotiation.md)
5253
1. [Enabling Several Formats](core/content-negotiation.md#enabling-several-formats)
5354
2. [Registering a Custom Serializer](core/content-negotiation.md#registering-a-custom-serializer)
5455
3. [Creating a Responder](core/content-negotiation.md#creating-a-responder)
55-
11. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
56-
12. [Extending JSON-LD context](core/extending-jsonld-context.md)
57-
13. [Data Providers](core/data-providers.md)
56+
12. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
57+
13. [Extending JSON-LD context](core/extending-jsonld-context.md)
58+
14. [Data Providers](core/data-providers.md)
5859
1. [Custom Collection Data Provider](core/data-providers.md#creating-a-custom-data-provider#custom-collection-data-provider)
5960
2. [Custom Item Data Provider](core/data-providers.md#returning-a-paged-collection#custom-item-data-provider)
60-
14. [Extensions](core/extensions.md)
61+
15. [Extensions](core/extensions.md)
6162
1. [Custom Extension](core/extensions.md#custom-extension)
6263
2. [Filter upon the current user](core/extensions.md#example)
6364
16. [Security](core/security.md)

0 commit comments

Comments
 (0)