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

Allow to display a custom error message #7761

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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