From 09edddca94484acd41369dbf40c0b63d501f28dc Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 15 Nov 2021 11:37:32 +0100 Subject: [PATCH] Introduce ModelManagerThrowable (#7602) * Introduce ModelManagerThrowable * Avoid BC-break * Update src/Controller/CRUDController.php Co-authored-by: Javier Spagnoletti Co-authored-by: Javier Spagnoletti --- src/Admin/LifecycleHookProviderInterface.php | 8 +-- src/Controller/CRUDController.php | 63 ++++++++++++++++++++ src/Exception/ModelManagerException.php | 2 +- src/Exception/ModelManagerThrowable.php | 18 ++++++ src/Model/ModelManagerInterface.php | 10 ++-- src/Util/ObjectAclManipulatorInterface.php | 4 +- 6 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 src/Exception/ModelManagerThrowable.php diff --git a/src/Admin/LifecycleHookProviderInterface.php b/src/Admin/LifecycleHookProviderInterface.php index 9248ff4d99..422a8afdb2 100644 --- a/src/Admin/LifecycleHookProviderInterface.php +++ b/src/Admin/LifecycleHookProviderInterface.php @@ -14,7 +14,7 @@ namespace Sonata\AdminBundle\Admin; use Sonata\AdminBundle\Exception\LockException; -use Sonata\AdminBundle\Exception\ModelManagerException; +use Sonata\AdminBundle\Exception\ModelManagerThrowable; /** * This interface can be implemented to provide hooks that will be called @@ -31,7 +31,7 @@ interface LifecycleHookProviderInterface * * @param object $object * - * @throws ModelManagerException + * @throws ModelManagerThrowable * @throws LockException * * @return object @@ -46,7 +46,7 @@ public function update($object); * * @param object $object * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @return object * @@ -60,7 +60,7 @@ public function create($object); * * @param object $object * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @phpstan-param T $object */ diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index ac77e278c3..ffad2692ae 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -21,6 +21,7 @@ use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; use Sonata\AdminBundle\Exception\LockException; use Sonata\AdminBundle\Exception\ModelManagerException; +use Sonata\AdminBundle\Exception\ModelManagerThrowable; use Sonata\AdminBundle\FieldDescription\FieldDescriptionCollection; use Sonata\AdminBundle\Templating\TemplateRegistryInterface; use Sonata\AdminBundle\Util\AdminObjectAclData; @@ -191,7 +192,16 @@ public function batchActionDelete(ProxyQueryInterface $query) $this->trans('flash_batch_delete_success', [], 'SonataAdminBundle') ); } catch (ModelManagerException $e) { + // NEXT_MAJOR: Remove this catch. $this->handleModelManagerException($e); + + $this->addFlash( + 'sonata_flash_error', + $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle') + ); + } catch (ModelManagerThrowable $e) { + $this->handleModelManagerThrowable($e); + $this->addFlash( 'sonata_flash_error', $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle') @@ -252,12 +262,28 @@ public function deleteAction($id) // NEXT_MAJOR: Remove the unused $id parameter ) ); } catch (ModelManagerException $e) { + // NEXT_MAJOR: Remove this catch. $this->handleModelManagerException($e); if ($this->isXmlHttpRequest()) { return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []); } + $this->addFlash( + 'sonata_flash_error', + $this->trans( + 'flash_delete_error', + ['%name%' => $this->escapeHtml($objectName)], + 'SonataAdminBundle' + ) + ); + } catch (ModelManagerThrowable $e) { + $this->handleModelManagerThrowable($e); + + if ($this->isXmlHttpRequest()) { + return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []); + } + $this->addFlash( 'sonata_flash_error', $this->trans( @@ -359,8 +385,13 @@ public function editAction($deprecatedId = null) // NEXT_MAJOR: Remove the unuse // redirect to edit mode return $this->redirectTo($existingObject); } catch (ModelManagerException $e) { + // NEXT_MAJOR: Remove this catch. $this->handleModelManagerException($e); + $isFormValid = false; + } catch (ModelManagerThrowable $e) { + $this->handleModelManagerThrowable($e); + $isFormValid = false; } catch (LockException $e) { $this->addFlash('sonata_flash_error', $this->trans('flash_lock_error', [ @@ -633,8 +664,13 @@ public function createAction() // redirect to edit mode return $this->redirectTo($newObject); } catch (ModelManagerException $e) { + // NEXT_MAJOR: Remove this catch. $this->handleModelManagerException($e); + $isFormValid = false; + } catch (ModelManagerThrowable $e) { + $this->handleModelManagerThrowable($e); + $isFormValid = false; } } @@ -1292,6 +1328,33 @@ protected function getBaseTemplate() * @throws \Exception */ protected function handleModelManagerException(\Exception $e) + { + if ($e instanceof ModelManagerThrowable) { + $this->handleModelManagerThrowable($e); + + return; + } + + @trigger_error(sprintf( + 'The method "%s()" is deprecated since sonata-project/admin-bundle 3.107 and will be removed in 5.0.', + __METHOD__ + ), \E_USER_DEPRECATED); + + if ($this->get('kernel')->isDebug()) { + throw $e; + } + + $context = ['exception' => $e]; + if ($e->getPrevious()) { + $context['previous_exception_message'] = $e->getPrevious()->getMessage(); + } + $this->getLogger()->error($e->getMessage(), $context); + } + + /** + * @throws ModelManagerThrowable + */ + protected function handleModelManagerThrowable(ModelManagerThrowable $e) { if ($this->get('kernel')->isDebug()) { throw $e; diff --git a/src/Exception/ModelManagerException.php b/src/Exception/ModelManagerException.php index 8303ccfd5a..56970c5d5c 100644 --- a/src/Exception/ModelManagerException.php +++ b/src/Exception/ModelManagerException.php @@ -18,6 +18,6 @@ * * @author Thomas Rabaix */ -class ModelManagerException extends \Exception +class ModelManagerException extends \Exception implements ModelManagerThrowable { } diff --git a/src/Exception/ModelManagerThrowable.php b/src/Exception/ModelManagerThrowable.php new file mode 100644 index 0000000000..9b3e91f1fc --- /dev/null +++ b/src/Exception/ModelManagerThrowable.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Exception; + +interface ModelManagerThrowable extends \Throwable +{ +} diff --git a/src/Model/ModelManagerInterface.php b/src/Model/ModelManagerInterface.php index 1a569978f2..7214b95800 100644 --- a/src/Model/ModelManagerInterface.php +++ b/src/Model/ModelManagerInterface.php @@ -15,7 +15,7 @@ use Sonata\AdminBundle\Datagrid\DatagridInterface; use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; -use Sonata\AdminBundle\Exception\ModelManagerException; +use Sonata\AdminBundle\Exception\ModelManagerThrowable; use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface; use Sonata\Exporter\Source\SourceIteratorInterface; @@ -49,7 +49,7 @@ public function getNewFieldDescriptionInstance($class, $name, array $options = [ /** * @param object $object * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @phpstan-param T $object */ @@ -58,7 +58,7 @@ public function create($object); /** * @param object $object * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @phpstan-param T $object */ @@ -67,7 +67,7 @@ public function update($object); /** * @param object $object * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @phpstan-param T $object */ @@ -109,7 +109,7 @@ public function find($class, $id); /** * @param string $class * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @phpstan-param class-string $class */ diff --git a/src/Util/ObjectAclManipulatorInterface.php b/src/Util/ObjectAclManipulatorInterface.php index de992a26fb..c834e48546 100644 --- a/src/Util/ObjectAclManipulatorInterface.php +++ b/src/Util/ObjectAclManipulatorInterface.php @@ -14,7 +14,7 @@ namespace Sonata\AdminBundle\Util; use Sonata\AdminBundle\Admin\AdminInterface; -use Sonata\AdminBundle\Exception\ModelManagerException; +use Sonata\AdminBundle\Exception\ModelManagerThrowable; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; @@ -28,7 +28,7 @@ interface ObjectAclManipulatorInterface * * @param AdminInterface $admin * - * @throws ModelManagerException + * @throws ModelManagerThrowable * * @return void */