ProviderDataTransformer: throw TransformationFailedException on error #2444
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Subject
A user of my Sonata application tried to submit a heic image in a field that only allows jpeg and png. He received a 500 Internal Server Error caused by an
UploadException
thrown byImageProvider
.I found out that
ImageProvider->doTransform()
is called twice. First byProviderDataTransformer
when the form is transformed to a model. Here the exception is caught and logged.Later, this method is invoked again by
BaseMediaEventSubscriber->prePersist()
. This exception is not caught and this causes the 500 Internal Server Error.ProviderDataTransformer
has this comment:I don't understand this comment because it just ignores the error and nothing is returned to the form. Also, as mentioned above, the
transform
method is called again later and the exception occurs anyway.My solution is to use the
TransformationFailedException
as suggested by the documentation ofDataTransformerInterface
:If I replace the log with this exception, a pretty exception is displayed:
I am targeting this branch, because it's backwards compatible.
Possible considerations:
TransformationFailedException
. I could also only transformUploadException
s and keep the current code to handle all other exceptions.TransformationFailedException
supports translations.Changelog
### Changed Throw TransformationFailedException when reverse transformation fails in ProviderDataTransformer.
`