From 3eb6817762c78d9eff1071f2c5562af339677fdc Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 5 Mar 2022 19:41:14 +0100 Subject: [PATCH] Allow to display a custom error message --- src/Controller/CRUDController.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index 67010cc61b..6f98bf0c82 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -169,11 +169,11 @@ public function batchActionDelete(ProxyQueryInterface $query): Response $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle') ); } catch (ModelManagerThrowable $e) { - $this->handleModelManagerThrowable($e); + $errorMessage = $this->handleModelManagerThrowable($e); $this->addFlash( 'sonata_flash_error', - $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle') + $errorMessage ?? $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle') ); } @@ -240,7 +240,7 @@ public function deleteAction(Request $request): Response ) ); } catch (ModelManagerThrowable $e) { - $this->handleModelManagerThrowable($e); + $errorMessage = $this->handleModelManagerThrowable($e); if ($this->isXmlHttpRequest($request)) { return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []); @@ -248,7 +248,7 @@ public function deleteAction(Request $request): Response $this->addFlash( 'sonata_flash_error', - $this->trans( + $errorMessage ?? $this->trans( 'flash_delete_error', ['%name%' => $this->escapeHtml($objectName)], 'SonataAdminBundle' @@ -334,7 +334,7 @@ public function editAction(Request $request): Response $isFormValid = false; } catch (ModelManagerThrowable $e) { - $this->handleModelManagerThrowable($e); + $errorMessage = $this->handleModelManagerThrowable($e); $isFormValid = false; } catch (LockException $e) { @@ -354,7 +354,7 @@ public function editAction(Request $request): Response $this->addFlash( 'sonata_flash_error', - $this->trans( + $errorMessage ?? $this->trans( 'flash_edit_error', ['%name%' => $this->escapeHtml($this->admin->toString($existingObject))], 'SonataAdminBundle' @@ -599,7 +599,7 @@ public function createAction(Request $request): Response $isFormValid = false; } catch (ModelManagerThrowable $e) { - $this->handleModelManagerThrowable($e); + $errorMessage = $this->handleModelManagerThrowable($e); $isFormValid = false; } @@ -613,7 +613,7 @@ public function createAction(Request $request): Response $this->addFlash( 'sonata_flash_error', - $this->trans( + $errorMessage ?? $this->trans( 'flash_create_error', ['%name%' => $this->escapeHtml($this->admin->toString($newObject))], 'SonataAdminBundle' @@ -1078,9 +1078,13 @@ protected function handleModelManagerException(\Exception $exception): void } /** + * NEXT_MAJOR: Add typehint. + * * @throws ModelManagerThrowable + * + * @return string|null A custom error message to display in the flag bag instead of the generic one */ - protected function handleModelManagerThrowable(ModelManagerThrowable $exception): void + protected function handleModelManagerThrowable(ModelManagerThrowable $exception) { $debug = $this->getParameter('kernel.debug'); \assert(\is_bool($debug)); @@ -1093,6 +1097,8 @@ protected function handleModelManagerThrowable(ModelManagerThrowable $exception) $context['previous_exception_message'] = $exception->getPrevious()->getMessage(); } $this->getLogger()->error($exception->getMessage(), $context); + + return null; } /**