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
+10-11Lines changed: 10 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,17 @@
1
-
# Error Handling
1
+
# Errors Handling
2
2
3
3
API Platform comes with a powerful error system. It handles excepted (such as faulty JSON documents sent by the
4
4
client or validation errors) as well as unexpected errors (PHP exceptions and errors).
5
-
API Platform automatically send the appropriate HTTP status code to the client: `400` for expected errors, `500` for
6
-
unexpected ones. It also provides a description of the respecting [the Hydra specification](http://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors)
7
-
or the [RFC 7807](https://tools.ietf.org/html/rfc7807) depending of the format selected during the [content negotiation](content-negotiation.md).
5
+
API Platform automatically sends the appropriate HTTP status code to the client: `400` for expected errors, `500` for
6
+
unexpected ones. It also provides a description of the error in [the Hydra error format](http://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).
8
8
9
9
## Converting PHP Exceptions to HTTP Errors
10
10
11
11
The framework also allows to configure the HTTP status code sent to the clients when custom exceptions are thrown.
12
12
13
-
In the following example, we will throw explain to throw a domain exception from the business layer of the application and
14
-
configure API Platform to convert it to a `404 Not Found` error. Let's create a this domain exception and the service throwing
15
-
it:
13
+
In the following example, we throw a domain exception from the business layer of the application and
14
+
configure API Platform to convert it to a `404 Not Found` error:
16
15
17
16
```php
18
17
<?php
@@ -63,14 +62,14 @@ final class ProductManager implements EventSubscriberInterface
63
62
}
64
63
```
65
64
66
-
If you use the standard distribution of API Platform, the event listener will be automatically registered. If you use a
65
+
If you use the standard distribution of API Platform, this event listener will be automatically registered. If you use a
67
66
custom installation, [learn how to register listeners](events.md).
68
67
69
68
Then, configure the framework to catch `AppBundle\Exception\ProductNotFoundException` exceptions and convert them in `404`
70
69
errors:
71
70
72
71
```yaml
73
-
#app/config/config.yml
72
+
# config/packages/api_platform.yaml
74
73
api_platform:
75
74
# ...
76
75
exception_to_status:
@@ -82,7 +81,7 @@ api_platform:
82
81
```
83
82
84
83
Any type of `Exception` can be thrown, API Platform will convert it to a Symfony's `HttpException`. The framework also takes
85
-
care to serialize the error description according to the request format. For instance, if the API should respond in JSON-LD,
84
+
care of serializing the error description according to the request format. For instance, if the API should respond in JSON-LD,
86
85
the error will be returned in this format as well:
87
86
88
87
`GET /products/1234`
@@ -94,4 +93,4 @@ the error will be returned in this format as well:
94
93
"hydra:title": "An error occurred",
95
94
"hydra:description": "The product \"1234\" does not exist."
0 commit comments