Skip to content

Instantly share code, notes, and snippets.

@nesk
Created November 29, 2018 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesk/2a4b9449a756352fb5472b71f6736f9c to your computer and use it in GitHub Desktop.
Save nesk/2a4b9449a756352fb5472b71f6736f9c to your computer and use it in GitHub Desktop.
Report an exception with Symfony without throwing
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/exception", name="exception")
*/
public function index(
RequestStack $requestStack,
HttpKernelInterface $httpKernel,
EventDispatcherInterface $dispatcher
) {
try {
throw new \Exception('derp');
} catch (\Exception $exception) {
$request = $requestStack->getCurrentRequest();
$type = $requestStack->getParentRequest()
? HttpKernelInterface::SUB_REQUEST
: HttpKernelInterface::MASTER_REQUEST;
$event = new GetResponseForExceptionEvent($httpKernel, $request, $type, $exception);
$dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment