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

Support custom ModelManagerException messages #8141

Merged
Show file tree
Hide file tree
Changes from 3 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
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" />
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
</php>
</phpunit>
21 changes: 9 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 @@ -1107,13 +1105,12 @@ protected function getBaseTemplate(): string

/**
* @throws \Exception
* @return string|null|void A custom error message to display in the flag bag instead of the generic one
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
*/
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
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
{
/**
* @psalm-suppress UndefinedInterfaceMethod
* @psalm-suppress PossiblyNullReference - end() can return null, apparently

Check failure on line 177 in src/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / Psalm

UnusedPsalmSuppress

src/DependencyInjection/Configuration.php:177:24: UnusedPsalmSuppress: This suppression is never used (see https://psalm.dev/207)
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
*
* @see https://github.com/psalm/psalm-plugin-symfony/issues/174
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Sonata\AdminBundle\Tests\App\Controller;

use Exception;
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,21 @@
<?php

declare(strict_types=1);

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
Loading