You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to add extra fields to the error response of application/json format like error code or metadata fields for specific purpose.
E.g. I'm building a messaging application. When user create a message, if it already exists, he/she will be redirected to that message page. Hence, I want to return a specific error code to help client-app determine the error type and an IRI of that Message resource to help client-app knows the Message Space they should redirect user to.
I created a MessageAlreadyExistsException and tried to add a custom normalization, which will decorate the ApiPlatform\Core\Problem\Serializer\ErrorNormalizer, to handle it.
final class MessageAlreadyExistsException extends \Exception implements ExceptionInterface
{
private const DEFAULT_MESSAGE = 'Message already exists.';
private const DEFAULT_ERROR_CODE = 'message_already_exists';
private string $errorCode;
private Message $message;
public function __construct(string $msg = null, Message $message, Throwable $previous = null)
{
parent::__construct($msg ?: self::DEFAULT_MESSAGE, 0, $previous);
$this->errorCode = $errorCode ?: self::DEFAULT_ERROR_CODE;
$this->message = $message;
}
public function getErrorCode(): string
{
return $this->errorCode;
}
public function getMessage(): Message
{
return $this->message;
}
}
But it doesn't work as expected. I figured out that whenever the exception is thrown and it comes to the normalizer, it becomes a FlattenException, instead of the MessageAlreadyExistsException. Hence, the JsonErrorNormalizer isn't able to normalize it. But I don't know how to resolve it.
I think the FlattenException's responsible to wrap all Exceptions before they are passed to normalization process, but I don't know why and where did they do that.
I'm looking for a help from an everyone. Thank you.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to add extra fields to the error response of
application/json
format like errorcode
or metadata fields for specific purpose.E.g. I'm building a messaging application. When user create a message, if it already exists, he/she will be redirected to that message page. Hence, I want to return a specific error code to help client-app determine the error type and an IRI of that Message resource to help client-app knows the Message Space they should redirect user to.
The expected error response look like below
I created a MessageAlreadyExistsException and tried to add a custom normalization, which will decorate the
ApiPlatform\Core\Problem\Serializer\ErrorNormalizer
, to handle it.But it doesn't work as expected. I figured out that whenever the exception is thrown and it comes to the normalizer, it becomes a
FlattenException
, instead of theMessageAlreadyExistsException
. Hence, theJsonErrorNormalizer
isn't able to normalize it. But I don't know how to resolve it.I think the FlattenException's responsible to wrap all Exceptions before they are passed to normalization process, but I don't know why and where did they do that.
I'm looking for a help from an everyone. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions