Skip to content

Commit c0daf0e

Browse files
authored
fix: multiple error routes #6214 (#6263)
1 parent 2945223 commit c0daf0e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Bundle/Resources/config/routing/api.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,15 @@
1414
<requirement key="index">index</requirement>
1515
</route>
1616

17+
<route id="api_errors" path="/errors/{status}">
18+
<default key="_controller">api_platform.action.not_exposed</default>
19+
<default key="status">500</default>
20+
21+
<requirement key="status">\d+</requirement>
22+
</route>
23+
24+
<route id="api_validation_errors" path="/validation_errors/{id}">
25+
<default key="_controller">api_platform.action.not_exposed</default>
26+
</route>
27+
1728
</routes>

Routing/IriConverter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ private function generateSymfonyRoute(object|string $resource, int $referenceTyp
186186
}
187187

188188
try {
189-
return $this->router->generate($operation->getName(), $identifiers, $operation->getUrlGenerationStrategy() ?? $referenceType);
189+
$routeName = $operation instanceof HttpOperation ? ($operation->getRouteName() ?? $operation->getName()) : $operation->getName();
190+
191+
return $this->router->generate($routeName, $identifiers, $operation->getUrlGenerationStrategy() ?? $referenceType);
190192
} catch (RoutingExceptionInterface $e) {
191193
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for the item of type "%s"', $operation->getClass()), $e->getCode(), $e);
192194
}

Validator/Exception/ValidationException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
operations: [
4141
new ErrorOperation(
4242
name: '_api_validation_errors_problem',
43+
routeName: 'api_validation_errors',
4344
outputFormats: ['json' => ['application/problem+json']],
4445
normalizationContext: ['groups' => ['json'],
4546
'skip_null_values' => true,
4647
'rfc_7807_compliant_errors' => true,
4748
]),
4849
new ErrorOperation(
4950
name: '_api_validation_errors_hydra',
51+
routeName: 'api_validation_errors',
5052
outputFormats: ['jsonld' => ['application/problem+json']],
5153
links: [new Link(rel: ContextBuilderInterface::JSONLD_NS.'error', href: 'http://www.w3.org/ns/hydra/error')],
5254
normalizationContext: [
@@ -57,9 +59,14 @@
5759
),
5860
new ErrorOperation(
5961
name: '_api_validation_errors_jsonapi',
62+
routeName: 'api_validation_errors',
6063
outputFormats: ['jsonapi' => ['application/vnd.api+json']],
6164
normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true, 'rfc_7807_compliant_errors' => true]
6265
),
66+
new ErrorOperation(
67+
name: '_api_validation_errors',
68+
routeName: 'api_validation_errors'
69+
),
6370
],
6471
graphQlOperations: []
6572
)]

0 commit comments

Comments
 (0)