You can implement an EventListener
that listen on the PDOException
:
// src/CM/PlatformBundlee/EventListener/PDOExceptionResponseListener.php
namespace CM\PlatformBundle\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\DBAL\Driver\PDOException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class PDOExceptionResponseListener
{
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelResponse(GetResponseForExceptionEvent $event)
{
$request = $event->getRequest();
$exception = $event->getException();
$message = $exception->getMessage();
// Listen only on the expected exception
if (!$exception instanceof PDOException) {
return;
}
// You can make some checks on the message to return a different response depending on the MySQL error given.
if (strpos($message, 'Integrity constraint violation')) {
// Add your user-friendly error message
$this->session->getFlashBag()->add('error', 'PDO Exception :'.$message);
}
}
}
Declare it as a service :
// app/config/services.yml
services:
acme.kernel.listener.pdo_exception_response_listener:
class: CM\PlatformBundle\EventListener\PDOExceptionResponseListener
tags:
- {name: kernel.event_listener, event: kernel.exception, method: onKernelResponse}
arguments:
session: "@session"
Make your template displaying session messages :
// twig
{% for flashMessage in app.session.flashbag.get('error') %}
{{ flashMessage }}
{% endfor %}
EDIT
If you want intercept this error on a specific action, you can do this :
try {
$em->flush();
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
// Add your message in the session
$this->get(‘session’)->getFlashBag()->add('error', 'PDO Exception :'.$errorMessage);
}