Calle Bergström
Calle Bergström

Reputation: 488

Symfony2 Debugger Stops Exception Propagation And Displays Error Page

I have a setup with multiple Symfony services that all throw exceptions on any error.

I have Controllers acting as wrappers for these services.

An action in one of my wrapper controllers (that my front-end send requests to) can look something like this:

/**
 * @Route("/someroute", name="some_route")
 */
public function myServiceWrapperFunctionAction(Request $request) {
    try {
        $this->get("myservice")->someFunctionThatThrowsException();
    } catch(Exception $e) {
        // This never happens when an exception is thrown from the service
        return new Response($e->getMessage());
    }
}

However I never seem to reach the catch block, instead the debugger is triggered like the try-catch wasn't even there.

Does the Symfony debugger intercept all exceptions that aren't caught within the same class? Or why isn't the exception message returned to my front-end in this case?

It feels like I am missing a really silly detail here.

Upvotes: 0

Views: 248

Answers (1)

Paweł Mikołajczuk
Paweł Mikołajczuk

Reputation: 3812

If you don't have

use Exception;

In your code, then proper try..catch should look like that:

try {
    $this->get("myservice")->someFunctionThatThrowsException();
} catch(\Exception $e) {
    // This never happens when an exception is thrown from the service
    return new Response($e->getMessage());
}

As just Exception will ask autoloader to load \Your\Controller\Namespace\Exception instead \Exception

Upvotes: 1

Related Questions