0

I'm having a problem with an exception in my Symfony2 controller... I'm trying to catch a NotFoundHttpException, but the catch block... just isn't happening, it goes to the standard Symfony2 exception page with the stack trace instead in the development environment...

I have the following code:

<?php

namespace SeerUK\DWright\GalleryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use SeerUK\DWright\GalleryBundle\Entity\Gallery;


class GalleryController extends Controller
{
    public function indexAction($galleryId)
    {
        try
        {
            $gallery = $this->getDoctrine()
                ->getRepository('SeerUKDWrightGalleryBundle:Gallery')
                ->find($galleryId);

            throw $this->createNotFoundException('rawr'); // Just for the sake of testing...

            if (!$gallery) {
                throw $this->createNotFoundException(
                    'No gallery found for id ' . $galleryId
                );
            }

            $galleryId          = $gallery->getId();
            $galleryName        = $gallery->getName();
            $galleryDesc        = $gallery->getDesc();
            $galleryPublishedOn = $gallery->getPublishedOn();

            return $this->render('SeerUKDWrightGalleryBundle:Gallery:index.html.twig', array(
                'galleryId'          => $galleryId,
                'galleryName'        => $galleryName,
                'galleryDesc'        => $galleryDesc,
                'galleryPublishedOn' => $galleryPublishedOn->format('Y-m-d H:i:s'),
            ));
        }
        catch (Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e)
        {
            echo $e->message;
        }
    }
}
0

1 Answer 1

1

You should try catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) or just use this NotFoundHttpException...

Probably it want to catch SeerUK\DWright\GalleryBundle\Controller\Symfony\Component\HttpKernel\Exception\NotFoundHttpException in your case :)

Sign up to request clarification or add additional context in comments.

1 Comment

You sir, were correct! It just needed the `` before the Symfony2 exception name :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.