forked from ecamp/ecamp3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: set rfc_7807_compliant_errors to true
- Loading branch information
Showing
8 changed files
with
145 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\DTO; | ||
|
||
use ApiPlatform\ApiResource\Error; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
|
||
class ValidationError extends Error { | ||
public function __construct( | ||
private readonly ?string $title, | ||
private readonly int $status, | ||
private readonly ?string $detail, | ||
private readonly ?string $instance, | ||
private readonly array $violations = [], | ||
private readonly string $type = 'about:blank', | ||
) { | ||
parent::__construct( | ||
type: $this->type, | ||
title: $this->title, | ||
status: $this->status, | ||
detail: $this->detail, | ||
instance: $this->instance, | ||
); | ||
} | ||
|
||
#[ApiProperty] | ||
public function getViolations(): array { | ||
return $this->violations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 0 additions & 85 deletions
85
api/src/Serializer/Normalizer/TranslationConstraintViolationListNormalizer.php
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
api/src/Serializer/Normalizer/ValidationErrorNormalizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Serializer\Normalizer; | ||
|
||
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait; | ||
use App\DTO\ValidationError; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
class ValidationErrorNormalizer implements NormalizerInterface { | ||
use ErrorNormalizerTrait; | ||
|
||
public const TYPE = 'type'; | ||
public const TITLE = 'title'; | ||
private array $defaultContext = [ | ||
self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10', | ||
self::TITLE => 'An error occurred', | ||
]; | ||
|
||
public function normalize(mixed $data, ?string $format = null, array $context = []): null|array|\ArrayObject|bool|float|int|string { | ||
return [ | ||
'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE], | ||
'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE], | ||
'detail' => $data->getDetail(), | ||
'violations' => $data->getViolations(), | ||
]; | ||
} | ||
|
||
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { | ||
return $data instanceof ValidationError; | ||
} | ||
|
||
public function getSupportedTypes(?string $format): array { | ||
return [ValidationError::class => true]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\State; | ||
|
||
use ApiPlatform\Metadata\HttpOperation; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ApiResource\Error; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Validator\Exception\ValidationException; | ||
use App\DTO\ValidationError; | ||
use App\Serializer\Normalizer\Error\TranslationInfoOfConstraintViolation; | ||
use App\Service\TranslateToAllLocalesService; | ||
|
||
/** | ||
* @psalm-suppress MissingTemplateParam | ||
*/ | ||
class ValidationErrorProvider implements ProviderInterface { | ||
public function __construct( | ||
private readonly ProviderInterface $decorated, | ||
private readonly TranslationInfoOfConstraintViolation $translationInfoOfConstraintViolation, | ||
private readonly TranslateToAllLocalesService $translateToAllLocalesService | ||
) {} | ||
|
||
/** | ||
* @psalm-suppress InvalidReturnStatement | ||
*/ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): null|array|object { | ||
$request = $context['request']; | ||
$exception = $request?->attributes->get('exception'); | ||
if (!($request ?? null) || !$operation instanceof HttpOperation || null === $exception) { | ||
throw new \RuntimeException('Not an HTTP request'); | ||
} | ||
|
||
$status = $operation->getStatus() ?? 500; | ||
$error = Error::createFromException($exception, $status); | ||
if (!$exception instanceof ValidationException) { | ||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
|
||
/** | ||
* @var ValidationException $exception | ||
*/ | ||
$violationInfos = []; | ||
foreach ($exception->getConstraintViolationList() as $violation) { | ||
$violationInfo = $this->translationInfoOfConstraintViolation->extract($violation); | ||
$translations = $this->translateToAllLocalesService->translate( | ||
$violation->getMessageTemplate(), | ||
array_merge( | ||
$violation->getPlural() ? ['%count%' => $violation->getPlural()] : [], | ||
$violation->getParameters() | ||
) | ||
); | ||
|
||
$violationInfos[] = [ | ||
'code' => $violation->getCode(), | ||
'propertyPath' => $violation->getPropertyPath(), | ||
'message' => $violation->getMessage(), | ||
'i18n' => [ | ||
...(array) $violationInfo, | ||
'translations' => $translations, | ||
], | ||
]; | ||
} | ||
|
||
return new ValidationError( | ||
type: $error->getType(), | ||
title: $error->getTitle(), | ||
status: $status, | ||
detail: $error->getDetail(), | ||
instance: $error->getInstance(), | ||
violations: $violationInfos | ||
); | ||
} | ||
} |
Oops, something went wrong.