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/content-negotiation.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ Using the raw JSON or raw XML formats is discouraged, prefer using JSON-LD inste
9
9
10
10
API Platform also supports [JSON Merge Patch (RFC 7396)](https://tools.ietf.org/html/rfc7396) the JSON:API[`PATCH`](https://tools.ietf.org/html/rfc5789) formats, as well as [Problem Details (RFC 7807)](https://tools.ietf.org/html/rfc7807), Hydra and JSON:API error formats.
11
11
12
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/formats?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="Formats screencast"><br>Watch the Formats screencast</a></p>
12
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/formats?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="Formats screencast"><br>Watch the Formats screencast</a></p>
13
13
14
14
API Platform will automatically detect the best resolving format depending on:
Copy file name to clipboardExpand all lines: core/errors.md
+138-3Lines changed: 138 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,39 @@ API Platform automatically sends the appropriate HTTP status code to the client:
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
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).
8
8
9
-
## Converting PHP Exceptions to HTTP Errors
9
+
# Errors
10
+
11
+
## Backward compatibility with < 3.1
12
+
13
+
Use the following configuration:
14
+
15
+
```yaml
16
+
api_platform:
17
+
defaults:
18
+
rfc_7807_compliant_errors: false
19
+
```
20
+
21
+
This can also be configured on an `ApiResource` or in an `HttpOperation`, for example:
There are many ways of configuring the exception status code we recommend reading the guides on how to use an [Error Provider](/docs/guides/error-provider) or create an [Error Resource](/docs/guides/error-resource).
30
+
31
+
1. we look at `exception_to_status` and take one if there's a match
32
+
2. If your exception is a `Symfony\Component\HttpKernel\Exception\HttpExceptionInterface` we get its status.
33
+
3. If the exception is a `ApiPlatform\Metadata\Exception\ProblemExceptionInterface` and there is a status we use it
34
+
4. Same for `ApiPlatform\Metadata\Exception\HttpExceptionInterface`
The framework also allows you to configure the HTTP status code sent to the clients when custom exceptions are thrown
12
44
on an API Platform resource operation.
@@ -101,14 +133,14 @@ the error will be returned in this format as well:
101
133
}
102
134
```
103
135
104
-
## Message Scope
136
+
### Message Scope
105
137
106
138
Depending on the status code you use, the message may be replaced with a generic one in production to avoid leaking unwanted information.
107
139
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.
108
140
109
141
In any other cases, your exception message will be sent to end users.
110
142
111
-
## Fine-grained Configuration
143
+
### Fine-grained Configuration
112
144
113
145
The `exceptionToStatus` configuration can be set on resources and operations:
114
146
@@ -140,3 +172,106 @@ class Book
140
172
141
173
Exceptions mappings defined on operations take precedence over mappings defined on resources, which take precedence over
142
174
the global config.
175
+
176
+
## Control your exceptions
177
+
178
+
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`.
179
+
180
+
To customize the API Platform response, replace the `api_platform.state.error_provider` with your own provider:
181
+
182
+
```php
183
+
<?php
184
+
185
+
namespace App\State;
186
+
187
+
use ApiPlatform\Metadata\Operation;
188
+
use ApiPlatform\State\ApiResource\Error;
189
+
use ApiPlatform\State\ProviderInterface;
190
+
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
191
+
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
Another way of having full control over domain exceptions is to create your own Error resource:
238
+
239
+
```php
240
+
<?php
241
+
242
+
namespace App\ApiResource;
243
+
244
+
use ApiPlatform\Metadata\ErrorResource;
245
+
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
246
+
247
+
#[ErrorResource]
248
+
class Error extends \Exception implements ProblemExceptionInterface
249
+
{
250
+
public function getType(): string
251
+
{
252
+
return 'teapot';
253
+
}
254
+
255
+
public function getTitle(): ?string
256
+
{
257
+
return null;
258
+
}
259
+
260
+
public function getStatus(): ?int
261
+
{
262
+
return 418;
263
+
}
264
+
265
+
public function getDetail(): ?string
266
+
{
267
+
return 'I am teapot';
268
+
}
269
+
270
+
public function getInstance(): ?string
271
+
{
272
+
return null;
273
+
}
274
+
}
275
+
```
276
+
277
+
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.
Copy file name to clipboardExpand all lines: core/extending-jsonld-context.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## JSON-LD
4
4
5
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/json-ld?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="JSON-LD screencast"><br>Watch the JSON-LD screencast</a></p>
5
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/json-ld?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="JSON-LD screencast"><br>Watch the JSON-LD screencast</a></p>
6
6
7
7
API Platform provides the possibility to extend the JSON-LD context of properties. This allows you to describe JSON-LD-typed
8
8
values, inverse properties using the `@reverse` keyword and you can even overwrite the `@id` property this way. Everything you define
@@ -63,7 +63,7 @@ Note that you do not have to provide the `@id` attribute. If you do not provide
63
63
64
64
## Hydra
65
65
66
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/hydra?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="Hydra screencast"><br>Watch the Hydra screencast</a></p>
66
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/hydra?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="Hydra screencast"><br>Watch the Hydra screencast</a></p>
67
67
68
68
It's also possible to replace the Hydra context used by the documentation generator:
Copy file name to clipboardExpand all lines: core/extending.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,4 +35,4 @@ For instance, if you want to send a mail after a resource has been persisted, bu
35
35
36
36
To replace existing API Platform services with your decorators, [check out how to decorate services](https://symfony.com/doc/current/service_container/service_decoration.html).
37
37
38
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform-security/service-decoration?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="Service Decoration screencast"><br>Watch the Service Decoration screencast</a></p>
38
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform-security/service-decoration?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="Service Decoration screencast"><br>Watch the Service Decoration screencast</a></p>
Copy file name to clipboardExpand all lines: core/filters.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ By default, all filters are disabled. They must be enabled explicitly.
12
12
When a filter is enabled, it automatically appears in the [OpenAPI](openapi.md) and [GraphQL](graphql.md) documentations.
13
13
It is also automatically documented as a `hydra:search` property for JSON-LD responses.
14
14
15
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/filters?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="Filtering and Searching screencast"><br>Watch the Filtering & Searching screencast</a></p>
15
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/filters?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="Filtering and Searching screencast"><br>Watch the Filtering & Searching screencast</a></p>
Copy file name to clipboardExpand all lines: core/getting-started.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ and what [JSON-LD](https://json-ld.org/) and [Hydra](https://www.hydra-cg.com/)
30
30
31
31
## Mapping the Entities
32
32
33
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/api-resource?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="Create an API Resource screencast"><br>Watch the Create an API Resource screencast</a></p>
33
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/api-resource?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="Create an API Resource screencast"><br>Watch the Create an API Resource screencast</a></p>
34
34
35
35
API Platform is able to automatically expose entities mapped as "API resources" through a REST API supporting CRUD
Copy file name to clipboardExpand all lines: core/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,6 @@ This bundle is extensively tested (unit and functional). The [`Fixtures/` direct
43
43
44
44
## Screencasts
45
45
46
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/tracks/rest?cid=apip#api-platform"><imgsrc="../distribution/images/symfonycasts-player.png"alt="SymfonyCasts, API Platform screencasts"></a></p>
46
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/tracks/rest?cid=apip#api-platform"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="SymfonyCasts, API Platform screencasts"></a></p>
47
47
48
48
The easiest and funniest way to learn how to use API Platform is to watch [the more than 60 screencasts available on SymfonyCasts](https://symfonycasts.com/tracks/rest?cid=apip#api-platform)!
Copy file name to clipboardExpand all lines: core/jwt.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ The tokens are signed by the server's key, so the server is able to verify that
7
7
8
8
API Platform allows to easily add a JWT-based authentication to your API using [LexikJWTAuthenticationBundle](https://github.com/lexik/LexikJWTAuthenticationBundle).
9
9
10
-
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/symfony-rest4/json-web-token?cid=apip"><imgsrc="../distribution/images/symfonycasts-player.png"alt="JWT screencast"><br>Watch the LexikJWTAuthenticationBundle screencast</a></p>
10
+
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/symfony-rest4/json-web-token?cid=apip"><imgsrc="/docs/distribution/images/symfonycasts-player.png"alt="JWT screencast"><br>Watch the LexikJWTAuthenticationBundle screencast</a></p>
11
11
12
12
## Installing LexikJWTAuthenticationBundle
13
13
@@ -116,7 +116,7 @@ also want to [configure Swagger UI for JWT authentication](#documenting-the-auth
116
116
117
117
### Adding Authentication to an API Which Uses a Path Prefix
118
118
119
-
If your API uses a [path prefix](https://symfony.com/doc/current/routing/external_resources.html#prefixing-the-urls-of-imported-routes), the security configuration would look something like this instead:
119
+
If your API uses a [path prefix](https://symfony.com/doc/current/routing/external_resources.html#route-groups-and-prefixes), the security configuration would look something like this instead:
0 commit comments