|
16 | 16 | use ApiPlatform\Api\IdentifiersExtractorInterface as LegacyIdentifiersExtractorInterface;
|
17 | 17 | use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
|
18 | 18 | use ApiPlatform\Metadata\Error as ErrorOperation;
|
| 19 | +use ApiPlatform\Metadata\Exception\HttpExceptionInterface; |
19 | 20 | use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
|
20 | 21 | use ApiPlatform\Metadata\HttpOperation;
|
21 | 22 | use ApiPlatform\Metadata\IdentifiersExtractorInterface;
|
|
32 | 33 | use Symfony\Component\HttpFoundation\Request;
|
33 | 34 | use Symfony\Component\HttpKernel\EventListener\ErrorListener as SymfonyErrorListener;
|
34 | 35 | use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
|
| 36 | +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; |
35 | 37 |
|
36 | 38 | /**
|
37 | 39 | * This error listener extends the Symfony one in order to add
|
@@ -71,6 +73,11 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
|
71 | 73 | $request->setRequestFormat($format);
|
72 | 74 | $apiOperation = $this->initializeOperation($request);
|
73 | 75 |
|
| 76 | + // TODO: add configuration flag to: |
| 77 | + // - always use symfony error handler (skips this listener) |
| 78 | + // - use symfony error handler if it's not an api error, ie apiOperation is null |
| 79 | + // - use api platform to handle errors (the default behavior we handle firewall errors for example but they're out of our scope) |
| 80 | + |
74 | 81 | // Let the error handler take this we don't handle HTML nor non-api platform requests
|
75 | 82 | if ('html' === $format) {
|
76 | 83 | $this->controller = 'error_controller';
|
@@ -139,6 +146,17 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
|
139 | 146 | $operation = $operation->withProvider('api_platform.state.error_provider');
|
140 | 147 | }
|
141 | 148 |
|
| 149 | + $normalizationContext = $operation->getNormalizationContext() ?? []; |
| 150 | + if (!($normalizationContext['_api_error_resource'] ?? false)) { |
| 151 | + $normalizationContext = $normalizationContext + ['api_error_resource' => true]; |
| 152 | + } |
| 153 | + |
| 154 | + if (!isset($normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES])) { |
| 155 | + $normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES] = ['trace', 'file', 'line', 'code', 'message', 'traceAsString']; |
| 156 | + } |
| 157 | + |
| 158 | + $operation = $operation->withNormalizationContext($normalizationContext); |
| 159 | + |
142 | 160 | $dup->attributes->set('_api_resource_class', $operation->getClass());
|
143 | 161 | $dup->attributes->set('_api_previous_operation', $apiOperation);
|
144 | 162 | $dup->attributes->set('_api_operation', $operation);
|
@@ -192,6 +210,14 @@ private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?
|
192 | 210 | return $exception->getStatusCode();
|
193 | 211 | }
|
194 | 212 |
|
| 213 | + if ($exception instanceof ProblemExceptionInterface && $status = $exception->getStatus()) { |
| 214 | + return $status; |
| 215 | + } |
| 216 | + |
| 217 | + if ($exception instanceof HttpExceptionInterface) { |
| 218 | + return $exception->getStatusCode(); |
| 219 | + } |
| 220 | + |
195 | 221 | if ($exception instanceof RequestExceptionInterface) {
|
196 | 222 | return 400;
|
197 | 223 | }
|
|
0 commit comments