sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Customise error message for ModelManagerException #8137

Closed wadjei closed 9 months ago

wadjei commented 9 months ago

Feature Request

Following from #7759, it is not possible to use the feature when the exception is of type ModelManagerException, even though these implement ModelManagerThrowable, as is the case for Doctrine UniqueConstraintViolationExceptions.

This is because, in CRUDController.php ModelManagerExceptions are caught first, before the catch for ModelManagerThrowable.

The relevant catches have been marked for removal for the last couple of years // NEXT_MAJOR: Remove this catch. but persist in the current 4.x and 5.x branches.

Currently, I'm having to hack around this issue by overriding addFlash, disabling the call to the CRUDController method for cases where I want to customise the message and storing the custom message in the admin (if there is one) so that I can retrieve it in the controller, before re-enabling the call to the CRUDController afterwards.

I see three possible clean resolutions to this:

  1. remove the catch blocks that have been earmarked since 2021 (this may have B.C. issues for some projects);
  2. swap the ordering of the ModelManagerThrowable and ModelManagerException catch blocks so that ModelManagerThrowable is handled first;
  3. apply a similar interface to handleModelManagerException so that it can also return a customised string.

I prefer option 3... is there any reason why this was not done at the same time as #7761 ?

wadjei commented 9 months ago

Hey @VincentLanglet I was having trouble getting handleModelManagerThrowable to customise the flashbag message on error and came up with the proposed PR https://github.com/sonata-project/SonataAdminBundle/pull/8141

WDYT?