Skip to content

Commit

Permalink
Allow to display a custom error message
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Mar 10, 2022
1 parent fd964de commit 74d5837
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

Expand Down Expand Up @@ -240,15 +240,15 @@ 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, []);
}

$this->addFlash(
'sonata_flash_error',
$this->trans(
$errorMessage ?? $this->trans(
'flash_delete_error',
['%name%' => $this->escapeHtml($objectName)],
'SonataAdminBundle'
Expand Down Expand Up @@ -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) {
Expand All @@ -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'
Expand Down Expand Up @@ -599,7 +599,7 @@ public function createAction(Request $request): Response

$isFormValid = false;
} catch (ModelManagerThrowable $e) {
$this->handleModelManagerThrowable($e);
$errorMessage = $this->handleModelManagerThrowable($e);

$isFormValid = false;
}
Expand All @@ -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'
Expand Down Expand Up @@ -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));
Expand All @@ -1093,6 +1097,8 @@ protected function handleModelManagerThrowable(ModelManagerThrowable $exception)
$context['previous_exception_message'] = $exception->getPrevious()->getMessage();
}
$this->getLogger()->error($exception->getMessage(), $context);

return null;
}

/**
Expand Down

0 comments on commit 74d5837

Please sign in to comment.