-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraintViolationException.php
51 lines (41 loc) · 1.28 KB
/
ConstraintViolationException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Validator;
use Exception;
use Symfony\Component\Validator\ConstraintViolationInterface;
use TheCodingMachine\GraphQLite\Exceptions\GraphQLExceptionInterface;
class ConstraintViolationException extends Exception implements GraphQLExceptionInterface
{
/** @var ConstraintViolationInterface */
private $violation;
public function __construct(ConstraintViolationInterface $violation)
{
parent::__construct((string) $violation->getMessage(), 400);
$this->violation = $violation;
}
/**
* Returns true when exception message is safe to be displayed to a client.
*/
public function isClientSafe(): bool
{
return true;
}
/**
* Returns the "extensions" object attached to the GraphQL error.
*
* @return array<string, mixed>
*/
public function getExtensions(): array
{
$extensions = ['category' => 'Validate'];
$code = $this->violation->getCode();
if (! empty($code)) {
$extensions['code'] = $code;
}
$propertyPath = $this->violation->getPropertyPath();
if (! empty($propertyPath)) {
$extensions['field'] = $propertyPath;
}
return $extensions;
}
}