You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/errors.md
+69-18Lines changed: 69 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ API Platform comes with a powerful error system. It handles expected (such as fa
4
4
client or validation errors) as well as unexpected errors (PHP exceptions and errors).
5
5
API Platform automatically sends the appropriate HTTP status code to the client: `400` for expected errors, `500` for
6
6
unexpected ones. It also provides a description of the error in [the Hydra error format](https://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors)
7
-
or in the format described in the [RFC 7807](https://tools.ietf.org/html/rfc7807), depending of the format selected during the [content negotiation](content-negotiation.md).
7
+
or in the format described in the [RFC 7807](https://tools.ietf.org/html/rfc7807), depending on the format selected during the [content negotiation](content-negotiation.md).
8
8
9
9
## Backward compatibility with < 3.1
10
10
@@ -25,16 +25,33 @@ This can also be configured on an `ApiResource` or in an `HttpOperation`, for ex
25
25
26
26
## Exception status code decision
27
27
28
-
There are many ways of configuring the exception status code we recommend reading the guides on how to use an [Error Provider](https://api-platform.com/docs/guides/error-provider/) or create an [Error Resource](https://api-platform.com/docs/guides/error-resource/).
28
+
There are many ways of configuring the exception status code we recommend reading the guides on how to use an
29
+
[Error Provider](https://api-platform.com/docs/guides/error-provider/) or create an [Error Resource](https://api-platform.com/docs/guides/error-resource/).
29
30
30
-
1. we look at `exception_to_status` and take one if there's a match
31
+
The decision works like this, if you are using API Platform with Symfony:
32
+
33
+
1. We look at `exception_to_status` and take one if there's a match
31
34
2. If your exception is a `Symfony\Component\HttpKernel\Exception\HttpExceptionInterface` we get its status.
32
35
3. If the exception is a `ApiPlatform\Metadata\Exception\ProblemExceptionInterface` and there is a status we use it
33
36
4. Same for `ApiPlatform\Metadata\Exception\HttpExceptionInterface`
34
-
5. We have some defaults `Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface` => 400 and `ApiPlatform\Validator\Exception\ValidationException` => 422
The framework also allows you to configure the HTTP status code sent to the clients when custom exceptions are thrown
@@ -45,7 +62,7 @@ configure API Platform to convert it to a `404 Not Found` error:
45
62
46
63
```php
47
64
<?php
48
-
// api/src/Exception/ProductNotFoundException.php
65
+
// api/src/Exception/ProductNotFoundException.php with Symfony or app/Exception/ProductNotFoundException.php with Laravel
49
66
namespace App\Exception;
50
67
51
68
final class ProductNotFoundException extends \Exception
@@ -56,11 +73,12 @@ final class ProductNotFoundException extends \Exception
56
73
57
74
```php
58
75
<?php
59
-
// api/src/EventSubscriber/ProductManager.php
76
+
// api/src/EventSubscriber/ProductManager.php with Symfony or app/EventSubscriber/ProductManager.php with Laravel
77
+
60
78
namespace App\EventSubscriber;
61
79
62
80
use ApiPlatform\EventListener\EventPriorities;
63
-
use App\Entity\Product;
81
+
use App\ApiResource\Product;
64
82
use App\Exception\ProductNotFoundException;
65
83
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
66
84
use Symfony\Component\HttpFoundation\Request;
@@ -92,11 +110,13 @@ final class ProductManager implements EventSubscriberInterface
92
110
```
93
111
94
112
If you use the standard distribution of API Platform, this event listener will be automatically registered. If you use a
95
-
custom installation, [learn how to register listeners](events.md#custom-event-listeners).
113
+
custom installation, [learn how to extend API Platform](extending.md).
96
114
97
115
Then, configure the framework to catch `App\Exception\ProductNotFoundException` exceptions and convert them into `404`
98
116
errors:
99
117
118
+
### Exception to status Configuration using Symfony
119
+
100
120
```yaml
101
121
# config/packages/api_platform.yaml
102
122
api_platform:
@@ -115,9 +135,32 @@ api_platform:
115
135
App\Exception\ProductNotFoundException: 404 # Here is the handler for our custom exception
116
136
```
117
137
118
-
Any type of `Exception` can be thrown, API Platform will convert it to a Symfony's `HttpException` (note that it means the exception will be flattened and lose all of its custom properties). The framework also takes
119
-
care of serializing the error description according to the request format. For instance, if the API should respond in JSON-LD,
120
-
the error will be returned in this format as well:
138
+
### Exception to status Configuration using Laravel
139
+
140
+
```php
141
+
<?php
142
+
// config/api-platform.php
143
+
return [
144
+
// ....
145
+
'exception_to_status' => [
146
+
// The 3 following handlers are registered by default, keep those lines to prevent unexpected side effects
App\Exception\ProductNotFoundException::class => 404 // Here is the handler for our custom exception
156
+
],
157
+
];
158
+
```
159
+
160
+
Any type of `Exception` can be thrown, API Platform will convert it to a Symfony's `HttpException` (note that it means
161
+
the exception will be flattened and lose all of its custom properties). The framework also takes care of serializing the
162
+
error description according to the request format. For instance, if the API should respond in JSON-LD, the error will be
163
+
returned in this format as well:
121
164
122
165
`GET /products/1234`
123
166
@@ -133,7 +176,9 @@ the error will be returned in this format as well:
133
176
### Message Scope
134
177
135
178
Depending on the status code you use, the message may be replaced with a generic one in production to avoid leaking unwanted information.
136
-
If your status code is >= 500 and < 600, the exception message will only be displayed in debug mode (dev and test). In production, a generic message matching the status code provided will be shown instead. If you are using an unofficial HTTP code, a general message will be displayed.
179
+
If your status code is >= 500 and < 600, the exception message will only be displayed in debug mode (dev and test).
180
+
In production, a generic message matching the status code provided will be shown instead. If you are using an unofficial
181
+
HTTP code, a general message will be displayed.
137
182
138
183
In any other cases, your exception message will be sent to end users.
139
184
@@ -143,8 +188,8 @@ The `exceptionToStatus` configuration can be set on resources and operations:
143
188
144
189
```php
145
190
<?php
146
-
// api/src/Entity/Book.php
147
-
namespace App\Entity;
191
+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
192
+
namespace App\ApiResource;
148
193
149
194
use ApiPlatform\Metadata\ApiResource;
150
195
use ApiPlatform\Metadata\Get;
@@ -172,7 +217,8 @@ the global config.
172
217
173
218
## Control your exceptions
174
219
175
-
With `rfc_7807_compliant_errors` a few things happen. First Hydra exception are compatible with the JSON Problem specification. Default exception that are handled by API Platform in JSON will be returned as `application/problem+json`.
220
+
With `rfc_7807_compliant_errors` a few things happen. First Hydra exception are compatible with the JSON Problem specification.
221
+
Default exception that are handled by API Platform in JSON will be returned as `application/problem+json`.
176
222
177
223
To customize the API Platform response, replace the `api_platform.state.error_provider` with your own provider:
178
224
@@ -202,7 +248,7 @@ final class ErrorProvider implements ProviderInterface
202
248
// You don't have to use this, you can use a Response, an array or any object (preferably a resource that API Platform can handle).
@@ -271,4 +319,7 @@ class Error extends \Exception implements ProblemExceptionInterface
271
319
}
272
320
```
273
321
274
-
We recommend using the `\ApiPlatform\Metadata\Exception\ProblemExceptionInterface` and the `\ApiPlatform\Metadata\Exception\HttpExceptionInterface`. For security reasons we add: `normalizationContext: ['ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString']]`because you usually don't want these. You can override this context value if you want.
322
+
We recommend using the `\ApiPlatform\Metadata\Exception\ProblemExceptionInterface` and the
323
+
`\ApiPlatform\Metadata\Exception\HttpExceptionInterface`. For security reasons we add: `normalizationContext: ['ignored_attributes'
324
+
=> ['trace', 'file', 'line', 'code', 'message', 'traceAsString']]`because you usually don't want these. You can override
0 commit comments