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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1107,13 +1105,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 @@ -1132,6 +1130,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 <thomas.rabaix@sonata-project.org>
*
* 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 <thomas.rabaix@sonata-project.org>
*
* 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;
}
}