Skip to content

Commit

Permalink
Support custom ModelManagerException messages (#8141)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Langlet <[email protected]>
  • Loading branch information
wadjei and VincentLanglet authored Dec 19, 2023
1 parent 1df71c0 commit 8b47fe7
Show file tree
Hide file tree
Showing 4 changed files with 557 additions and 12 deletions.
24 changes: 12 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 @@ -1104,13 +1102,13 @@ protected function getBaseTemplate(): string

/**
* @throws \Exception
*
* @return string|null A custom error message to display in the flag bag instead of the generic one
*/
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 All @@ -1129,6 +1127,8 @@ protected function handleModelManagerException(\Exception $exception): void
$context['previous_exception_message'] = $exception->getPrevious()->getMessage();
}
$this->getLogger()->error($exception->getMessage(), $context);

return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\App\Controller;

use Sonata\AdminBundle\Controller\CRUDController;

/**
* @phpstan-extends CRUDController<object>
*/
final class CustomModelManagerExceptionMessageController extends CRUDController
{
public const ERROR_MESSAGE = 'message from model manager exception';

protected function handleModelManagerException(\Exception $exception): string
{
return self::ERROR_MESSAGE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\App\Controller;

use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Exception\ModelManagerThrowable;

/**
* @phpstan-extends CRUDController<object>
*/
final class CustomModelManagerThrowableMessageController extends CRUDController
{
public const ERROR_MESSAGE = 'message from model manager throwable';

protected function handleModelManagerThrowable(ModelManagerThrowable $exception): string
{
return self::ERROR_MESSAGE;
}
}
Loading

0 comments on commit 8b47fe7

Please sign in to comment.