Skip to content

Commit

Permalink
Introduce ModelManagerThrowable (#7602)
Browse files Browse the repository at this point in the history
* Introduce ModelManagerThrowable

* Avoid BC-break

* Update src/Controller/CRUDController.php

Co-authored-by: Javier Spagnoletti <phansys@gmail.com>

Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
  • Loading branch information
VincentLanglet and phansys authored Nov 15, 2021
1 parent 6d6b2d6 commit 09edddc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/Admin/LifecycleHookProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Sonata\AdminBundle\Admin;

use Sonata\AdminBundle\Exception\LockException;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Sonata\AdminBundle\Exception\ModelManagerThrowable;

/**
* This interface can be implemented to provide hooks that will be called
Expand All @@ -31,7 +31,7 @@ interface LifecycleHookProviderInterface
*
* @param object $object
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
* @throws LockException
*
* @return object
Expand All @@ -46,7 +46,7 @@ public function update($object);
*
* @param object $object
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @return object
*
Expand All @@ -60,7 +60,7 @@ public function create($object);
*
* @param object $object
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @phpstan-param T $object
*/
Expand Down
63 changes: 63 additions & 0 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Exception\LockException;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Sonata\AdminBundle\Exception\ModelManagerThrowable;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionCollection;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\AdminBundle\Util\AdminObjectAclData;
Expand Down Expand Up @@ -191,7 +192,16 @@ public function batchActionDelete(ProxyQueryInterface $query)
$this->trans('flash_batch_delete_success', [], 'SonataAdminBundle')
);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);

$this->addFlash(
'sonata_flash_error',
$this->trans('flash_batch_delete_error', [], 'SonataAdminBundle')
);
} catch (ModelManagerThrowable $e) {
$this->handleModelManagerThrowable($e);

$this->addFlash(
'sonata_flash_error',
$this->trans('flash_batch_delete_error', [], 'SonataAdminBundle')
Expand Down Expand Up @@ -252,12 +262,28 @@ public function deleteAction($id) // NEXT_MAJOR: Remove the unused $id parameter
)
);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);

if ($this->isXmlHttpRequest()) {
return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []);
}

$this->addFlash(
'sonata_flash_error',
$this->trans(
'flash_delete_error',
['%name%' => $this->escapeHtml($objectName)],
'SonataAdminBundle'
)
);
} catch (ModelManagerThrowable $e) {
$this->handleModelManagerThrowable($e);

if ($this->isXmlHttpRequest()) {
return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []);
}

$this->addFlash(
'sonata_flash_error',
$this->trans(
Expand Down Expand Up @@ -359,8 +385,13 @@ public function editAction($deprecatedId = null) // NEXT_MAJOR: Remove the unuse
// redirect to edit mode
return $this->redirectTo($existingObject);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);

$isFormValid = false;
} catch (ModelManagerThrowable $e) {
$this->handleModelManagerThrowable($e);

$isFormValid = false;
} catch (LockException $e) {
$this->addFlash('sonata_flash_error', $this->trans('flash_lock_error', [
Expand Down Expand Up @@ -633,8 +664,13 @@ public function createAction()
// redirect to edit mode
return $this->redirectTo($newObject);
} catch (ModelManagerException $e) {
// NEXT_MAJOR: Remove this catch.
$this->handleModelManagerException($e);

$isFormValid = false;
} catch (ModelManagerThrowable $e) {
$this->handleModelManagerThrowable($e);

$isFormValid = false;
}
}
Expand Down Expand Up @@ -1292,6 +1328,33 @@ protected function getBaseTemplate()
* @throws \Exception
*/
protected function handleModelManagerException(\Exception $e)
{
if ($e instanceof ModelManagerThrowable) {
$this->handleModelManagerThrowable($e);

return;
}

@trigger_error(sprintf(
'The method "%s()" is deprecated since sonata-project/admin-bundle 3.107 and will be removed in 5.0.',
__METHOD__
), \E_USER_DEPRECATED);

if ($this->get('kernel')->isDebug()) {
throw $e;
}

$context = ['exception' => $e];
if ($e->getPrevious()) {
$context['previous_exception_message'] = $e->getPrevious()->getMessage();
}
$this->getLogger()->error($e->getMessage(), $context);
}

/**
* @throws ModelManagerThrowable
*/
protected function handleModelManagerThrowable(ModelManagerThrowable $e)
{
if ($this->get('kernel')->isDebug()) {
throw $e;
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ModelManagerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ModelManagerException extends \Exception
class ModelManagerException extends \Exception implements ModelManagerThrowable
{
}
18 changes: 18 additions & 0 deletions src/Exception/ModelManagerThrowable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Exception;

interface ModelManagerThrowable extends \Throwable
{
}
10 changes: 5 additions & 5 deletions src/Model/ModelManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Sonata\AdminBundle\Exception\ModelManagerThrowable;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\Exporter\Source\SourceIteratorInterface;

Expand Down Expand Up @@ -49,7 +49,7 @@ public function getNewFieldDescriptionInstance($class, $name, array $options = [
/**
* @param object $object
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @phpstan-param T $object
*/
Expand All @@ -58,7 +58,7 @@ public function create($object);
/**
* @param object $object
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @phpstan-param T $object
*/
Expand All @@ -67,7 +67,7 @@ public function update($object);
/**
* @param object $object
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @phpstan-param T $object
*/
Expand Down Expand Up @@ -109,7 +109,7 @@ public function find($class, $id);
/**
* @param string $class
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @phpstan-param class-string<T> $class
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Util/ObjectAclManipulatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Sonata\AdminBundle\Util;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Sonata\AdminBundle\Exception\ModelManagerThrowable;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;

Expand All @@ -28,7 +28,7 @@ interface ObjectAclManipulatorInterface
*
* @param AdminInterface<object> $admin
*
* @throws ModelManagerException
* @throws ModelManagerThrowable
*
* @return void
*/
Expand Down

0 comments on commit 09edddc

Please sign in to comment.