Skip to content

Commit

Permalink
Add support for customising ModelManagerException messages (sonata-pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
wadjei committed Dec 13, 2023
1 parent 636f338 commit 097db6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ It's auto-generated by sonata-project/dev-kit package.
<env name="KERNEL_CLASS" value="\Sonata\AdminBundle\Tests\App\AppKernel" />
<server name="APP_ENV" value="test" force="true" />
<server name="APP_DEBUG" value="false" />
<ini name="memory_limit" value="256M" />
</php>
</phpunit>
20 changes: 8 additions & 12 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ public function batchActionDelete(ProxyQueryInterface $query): Response
);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);

$errorMessage = $this->handleModelManagerException($e);
$this->addFlash(
'sonata_flash_error',
$this->trans('flash_batch_delete_error', [], 'SonataAdminBundle')
$errorMessage ?? $this->trans('flash_batch_delete_error', [], 'SonataAdminBundle')
);
} catch (ModelManagerThrowable $e) {
$errorMessage = $this->handleModelManagerThrowable($e);
Expand Down Expand Up @@ -229,15 +228,15 @@ public function deleteAction(Request $request): Response
);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);
$errorMessage = $this->handleModelManagerException($e);

if ($this->isXmlHttpRequest($request)) {
return $this->renderJson(['result' => 'error']);
}

$this->addFlash(
'sonata_flash_error',
$this->trans(
$errorMessage ?? $this->trans(
'flash_delete_error',
['%name%' => $this->escapeHtml($objectName)],
'SonataAdminBundle'
Expand Down Expand Up @@ -295,7 +294,6 @@ public function editAction(Request $request): Response
if (null !== $preResponse) {
return $preResponse;
}

$this->admin->setSubject($existingObject);
$objectId = $this->admin->getNormalizedIdentifier($existingObject);
\assert(null !== $objectId);
Expand Down Expand Up @@ -334,7 +332,7 @@ public function editAction(Request $request): Response
return $this->redirectTo($request, $existingObject);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);
$errorMessage = $this->handleModelManagerException($e);

$isFormValid = false;
} catch (ModelManagerThrowable $e) {
Expand Down Expand Up @@ -610,7 +608,7 @@ public function createAction(Request $request): Response
return $this->redirectTo($request, $newObject);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);
$errorMessage = $this->handleModelManagerException($e);

$isFormValid = false;
} catch (ModelManagerThrowable $e) {
Expand Down Expand Up @@ -1108,12 +1106,10 @@ protected function getBaseTemplate(): string
/**
* @throws \Exception
*/
protected function handleModelManagerException(\Exception $exception): void
protected function handleModelManagerException(\Exception $exception)
{
if ($exception instanceof ModelManagerThrowable) {
$this->handleModelManagerThrowable($exception);

return;
return $this->handleModelManagerThrowable($exception);
}

@trigger_error(sprintf(
Expand Down

0 comments on commit 097db6f

Please sign in to comment.