Custom data in error response #2631
Closed
SerheyDolgushev
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
Hello, the detail part of the specification has:
Therefore you should not use
I'd declare an error resource as it's cleaner to have a domain exception that is also an Error api-wise. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem
For errored requests, API Platform returns the string representation of the error in the
detail
field. Which works fine for most cases. However, in some cases, additional information about the error needs to be returned as a non-string structure.Steps to reproduce
Let's assume there is following exception in the system (it stores an array of strings in
errorCodes
property):In some cases one of the API Platform Processors throws this exception:
The following code will be executed once this exception is thrown:
ApiPlatform\Symfony\EventListener\ErrorListener::duplicateRequest
will return a duplicate request.ApiPlatform\Action\ExceptionAction::__invoke
will be used to handle step 1. And it will convert exception to FlattenException. At this pointerrorCodes
field of originalExceptionWithErrorCodes
will be lost.ApiPlatform\Problem\Serializer\ErrorNormalizer::normalize
will be used to convert exception from the previous step into the response.Human-readable message
. But['CODE1', 'CODE2']
of the original exception will be not reflected in the response.Currently Possible Solutions
Solution 1
ApiPlatform\Action\ExceptionAction
with the following changes:public function __invoke(FlattenException $exception, Request $request): Response
=>
public function __invoke(\Throwable $exception, Request $request): Response
errorCodes
from$exception
and pass it as part of$context
into serializererrorCodes
from the$context
and inject it into the response:Solution 2
ExceptionWithErrorCodes
and store the serialized error codes in its message (as string).ExceptionWithErrorCodes
, and if so it will return error codes from it's unserialized message.Goal of this discussion
Solution 1 seems to be the less hacky one. But it requires overriding
ApiPlatform\Action\ExceptionAction
and using internal traits/classes. The main goal of this discussion is to start a conversation and discuss if that is a good idea to modifyApiPlatform\Action\ExceptionAction
so it can be easily extended (without using internal traits/classes) in cases like this one.Beta Was this translation helpful? Give feedback.
All reactions