Skip to content

Instantly share code, notes, and snippets.

@netgusto
Created June 2, 2014 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netgusto/bf2b0bae42ee4bffe8a1 to your computer and use it in GitHub Desktop.
Save netgusto/bf2b0bae42ee4bffe8a1 to your computer and use it in GitHub Desktop.
Symfony: Interception des exceptions
<?php
// My/CoreBundle/EventListener/ExceptionEventListener.php
namespace My\CoreBundle\EventListener;
use Symfony\Bundle\FrameworkBundle\Routing\Router,
Symfony\Bundle\FrameworkBundle\Translation\Translator,
Symfony\Component\HttpFoundation\Session\Session,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\RedirectResponse,
Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Twig_Environment as TwigEnvironment;
class ExceptionEventListener {
protected $router;
protected $twig;
protected $session;
protected $mailer;
protected $fromEmail;
protected $fromName;
protected $to;
public function __construct(Router $router, TwigEnvironment $twig, Session $session, Translator $translator, \Swift_Mailer $mailer, $fromEmail, $fromName, array $to) {
$this->router = $router;
$this->twig = $twig;
$this->session = $session;
$this->translator = $translator;
$this->mailer = $mailer;
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
$this->to = $to;
}
public function onKernelException(GetResponseForExceptionEvent $event) {
# On récupère l'exception
$exception = $event->getException();
# La requête HTTP
$request = $event->getRequest();
# Le client associé au sous-domaine courant
$garage = $request->attributes->get('client');
$subject = "Une exception s'est produite";
$msgbody = $this->twig->render('MyCoreBundle:Mail/Exception:generic.html.twig', array(
'exception' => $exception,
'request' => $request,
));
# 1. On informe les administrateurs du problème
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom(array($this->fromEmail => $this->fromName))
->setTo($this->to)
->setBody($msgbody, 'text/html');
$this->mailer->send($message);
# 2. On redirige l'utilisateur vers la page d'erreur 500 de l'application
$redirectUri = $this->router->generate('route_erreur500');
$event->setResponse(new RedirectResponse($redirectUri));
}
}
{# My/CoreBundle/Resources/views/Mail/Exception/generic.html.twig #}
{% if request.attributes.get('client') is defined %}
<h1>Une erreur s'est produite sur la plateforme du client "{{ request.attributes.get('client').name }}"</h1>
{% else %}
<h1>Une erreur s'est produite</h1>
{% endif %}
<h2>Informations</h2>
<p style="color: red;"><strong>Exception:</strong> {{ exception.message }}</p>
<p><strong>Horodatage:</strong> {{ 'now'|date('d/m/Y à H:i:s') }}</p>
<p><strong>URL:</strong> {{ request.scheme ~ '://' ~ request.httpHost ~ request.basePath ~ request.pathInfo }}</p>
<p><strong>IP utilisateur:</strong> {{ request.clientIp }}</p>
<h2>Pile d'appel</h2>
<pre>{{ exception.traceAsString }}</pre>
# My/CoreBundle/Resources/config/services.yml
services:
netgusto.listener.exception:
class: My\CoreBundle\EventListener\ExceptionEventListener
arguments:
router: @router
twig: @twig
session: @session
translator: @translator
mailer: @mailer
fromEmail: contact@netgusto.com
fromName: E-Space
to:
- contact@netgusto.com
- webmaster@netgusto.com
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment