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
API Platform Core allows to customize the HTTP status code sent to the clients when exceptions are thrown.
3
+
API Platform comes with a powerful error system. It handles excepted (such as faulty JSON documents sent by the
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).
4
8
5
-
```yaml
6
-
# app/config/config.yml
7
-
8
-
api_platform:
9
-
# Map exceptions to HTTP status codes using the `exception_to_status` configuration key
10
-
exception_to_status:
11
-
# The 2 following exceptions are handled by default
12
-
Symfony\Component\Serializer\Exception\ExceptionInterface: 400# Use a raw status code (recommended)
13
-
ApiPlatform\Core\Exception\InvalidArgumentException: 'HTTP_BAD_REQUEST'# Or with a constant of `Symfony\Component\HttpFoundation\Response`
9
+
## Converting PHP Exceptions to HTTP Errors
14
10
15
-
AppBundle\Exception\ProductNotFoundException: 404# Custom exceptions can easily be handled
16
-
```
11
+
The framework also allows to configure the HTTP status code sent to the clients when custom exceptions are thrown.
17
12
18
-
As in any php application, your exceptions have to extends the \Exception class or any of it's children.
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
public function checkProductAvailability(GetResponseForControllerResultEvent $event)
51
+
public function checkProductAvailability(GetResponseForControllerResultEvent $event): void
61
52
{
62
53
$product = $event->getControllerResult();
63
-
$method = $event->getRequest()->getMethod();
64
-
65
-
if (!$product instanceof Product || Request::METHOD_GET !== $method) {
54
+
if (!$product instanceof Product || !$event->getRequest()->isMethodSafe(false)) {
66
55
return;
67
56
}
68
57
69
-
if (!$product->getVirtualStock()) {
58
+
if (!$product->isPubliclyAvailable()) {
70
59
// Using internal codes for a better understanding of what's going on
71
-
throw new ProductNotFoundException(self::OUTOFSTOCK);
60
+
throw new ProductNotFoundException(sprintf('The product "%s" does not exist.', $product->getId()));
72
61
}
73
62
}
74
63
}
75
64
```
76
65
77
-
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, if the API is configured to respond in JSON-LD, the error will be returned in this format as well.
78
-
79
-
```json
80
-
{
81
-
"@context": "/contexts/Error",
82
-
"@type": "Error",
83
-
"hydra:title": "An error occurred",
84
-
"hydra:description": "53"
85
-
}
86
-
```
87
-
88
-
Is what you get, with an HTTP status code 404 as defined in the configuration.
89
-
90
-
## Validation errors
91
-
92
-
API Platform does handle the validation errors responses for you. You can define a Symfony supported constraint, or a custom constraint upon any `ApiResource` or it's properties.
93
-
94
-
```php
95
-
<?php
96
-
97
-
// src/AppBundle/Entity/Product.php
98
-
99
-
namespace AppBundle\Entity;
100
-
101
-
use ApiPlatform\Core\Annotation\ApiResource;
102
-
use Doctrine\ORM\Mapping as ORM;
103
-
use Symfony\Component\Validator\Constraints as Assert;
API Platform will handle the error returned and adapt it's format according to the API configuration. If you did configured it to respond in JSON-LD. Your response would looks like:
88
+
`GET /products/1234`
189
89
190
90
```json
191
91
{
192
-
"@context": "/contexts/ConstraintViolationList",
193
-
"@type": "ConstraintViolationList",
92
+
"@context": "/contexts/Error",
93
+
"@type": "Error",
194
94
"hydra:title": "An error occurred",
195
-
"hydra:description": "properties: The product must have the minimal properties required (description, price)",
196
-
"violations": [
197
-
{
198
-
"propertyPath": "properties",
199
-
"message": "The product must have the minimal properties required (description, price)"
200
-
}
201
-
]
95
+
"hydra:description": "The product \"1234\" does not exist."
Copy file name to clipboardExpand all lines: core/validation.md
+125-5Lines changed: 125 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,134 @@
1
1
# Validation
2
2
3
-
API Platform Core uses the [Symfony Validator component](http://symfony.com/doc/current/book/validation.html) to validate
4
-
entities.
3
+
API Platform take care of validating data sent to the API by the client (usually user data entered through forms).
4
+
By default, the framework relies on [the powerful Symfony Validator Component](http://symfony.com/doc/current/validation.html)
5
+
for this task, but you can replace it by your preferred validation library such as [the PHP filter extension](http://php.net/manual/en/intro.filter.php)
6
+
if you want to.
5
7
6
-
Without specific configuration, it uses the default validation group, but this behavior is customizable.
8
+
## Validating Submitted Data
9
+
10
+
Validating submitted data is simple as adding [Symfony's builtin constraints](http://symfony.com/doc/current/reference/constraints.html)
11
+
or [custom constraints](http://symfony.com/doc/current/validation/custom_constraint.html) directly in classes marked with
12
+
the `@ApiResource` annotation:
13
+
14
+
```php
15
+
<?php
16
+
// src/AppBundle/Entity/Product.php
17
+
18
+
namespace AppBundle\Entity;
19
+
20
+
use ApiPlatform\Core\Annotation\ApiResource;
21
+
use Doctrine\ORM\Mapping as ORM;
22
+
use Symfony\Component\Validator\Constraints as Assert; // Symfony's builtin constraints
23
+
use AppBundle\Validator\Constraints\MinimalProperties; // A custom constraint
24
+
25
+
/**
26
+
* A product.
27
+
*
28
+
* @ApiResource
29
+
* @ORM\Entity
30
+
*/
31
+
class Product
32
+
{
33
+
/**
34
+
* @var int The id of this product.
35
+
*
36
+
* @ORM\Id
37
+
* @ORM\GeneratedValue
38
+
* @ORM\Column(type="integer")
39
+
*/
40
+
private $id;
41
+
42
+
/**
43
+
* @var string The name of the product
44
+
*
45
+
* @Assert\NotBlank
46
+
* @ORM\Column
47
+
*/
48
+
private name;
49
+
50
+
/**
51
+
* @var string[] Describe the product
52
+
*
53
+
* @MinimalProperties
54
+
* @ORM\Column(type="json")
55
+
*/
56
+
private $properties;
57
+
58
+
// Getters and setters...
59
+
}
60
+
```
61
+
62
+
Here is a custom constraint and the related validator:
0 commit comments