brpaz
brpaz

Reputation: 3658

symfony2 throwing exception from onKernelRequest returns blank page

I have an event listener hooked to kernel.request event. In the onKernel request method i am making some validations based on the subdomain. If this validations fail I want to throw some exception like AccessDenied or something.

The problem is when I throw the exception it shows a blank page instead of my custom error page.

If I check my prod.log file I get the following info:

[2013-06-06 11:08:38] request.ERROR: Exception thrown when handling an exception (Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: This route needs to be accessed with a subdomain) [] []
[2013-06-06 11:16:32] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: "This route needs to be accessed with a subdomain" at (...) line 86 [] [] 

What i am missing? Thank you for your help

Upvotes: 2

Views: 1548

Answers (1)

gblock
gblock

Reputation: 604

Exceptions are not catched during kernel.request events. You can return a response instead:

class SubdomainSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            'kernel.request' => 'onRequest',
        ];
    }

    public function onRequest(GetResponseEvent $event)
    {
        if (!$subdomainValid) {
            $event->setResponse(new Response('Invalid subdomain!', 403));
        }
    }
}

Although this example is an EventSubscriber it works with a listener, too.

Upvotes: 4

Related Questions