Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProviderDataTransformer: throw TransformationFailedException on error #2444

Merged

Conversation

jorrit
Copy link
Contributor

@jorrit jorrit commented Feb 14, 2024

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 by ImageProvider.

I found out that ImageProvider->doTransform() is called twice. First by ProviderDataTransformer 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:

// #1107 We must never throw an exception here.
// An exception here would prevent us to provide meaningful errors through the Form
// Error message inspired from Monolog\ErrorHandler

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 of DataTransformerInterface:

* @throws TransformationFailedException when the transformation fails

If I replace the log with this exception, a pretty exception is displayed:

image

I am targeting this branch, because it's backwards compatible.

Possible considerations:

  • I convert all exceptions to a TransformationFailedException. I could also only transform UploadExceptions and keep the current code to handle all other exceptions.
  • The error messages are not translated. This could be improved because TransformationFailedException supports translations.

Changelog

### Changed

Throw TransformationFailedException when reverse transformation fails in ProviderDataTransformer.

`

@jorrit jorrit force-pushed the transformationfailedexception branch 2 times, most recently from dde6040 to 43989ac Compare March 4, 2024 08:19
@jorrit
Copy link
Contributor Author

jorrit commented Mar 4, 2024

I can't find the button to request a review. Is that because some of the checks fail? The failing checks are not related to my code.

Can someone take a look at this PR? @VincentLanglet @jordisala1991 Thanks!

@VincentLanglet
Copy link
Member

This was introduced by #1246 which seems pretty old and was presented as a bugfix by @lemoinem not sure if he can review this.

@jorrit can you confirm that:

  • If you remove the try/catch you get a 500
  • If you try/catch and throw a TransformationFailedException you get a 200

If so, it means that maybe this was the right fix to do seven years ago ^^'

@jorrit
Copy link
Contributor Author

jorrit commented Mar 5, 2024

I confirm this. I upload a non-image to an image field and get a 500 Internal Server Error. I change the catch block in ProviderDataTransformer to throw new TransformationFailedException($e->getMessage(), 0, $e, $e->getMessage()); and I get a friendly message.

@VincentLanglet
Copy link
Member

You can rebase 4.x the fix is build by #2446

@jorrit jorrit force-pushed the transformationfailedexception branch from 43989ac to 9d95823 Compare March 5, 2024 08:05
@jorrit
Copy link
Contributor Author

jorrit commented Mar 5, 2024

Done, and thanks for fixing the errors. It is always good to see a green checkmark.

{
use LoggerAwareTrait;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a BC break.

If someone called setLogger it will crash.

Might be better to keep LoggerAwareInterface and LoggerAwareTrait with a NEXT_MAJOR comment "Remove them".

@jorrit jorrit force-pushed the transformationfailedexception branch from 9d95823 to 2637e91 Compare March 5, 2024 13:55
@VincentLanglet VincentLanglet merged commit ab5530f into sonata-project:4.x Mar 8, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants