I am wondering how would I simulate a 500 error in Symfony 2.
I have been reading this post where Raise suggests throwing an exception
throw new sfException('Testing the 500 error');
in Symfony 1.4.
I have been placing this code in my
\store\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\Controller\ExceptionController.php
but I get the fatal error
Class 'Symfony\Bundle\TwigBundle\Controller\sfException' not found in /home/notroot/www/store/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php on line 49`
Line 49 refers to the exception code I added.
My question is if throwing an exception is still viable in forcing a 500 error in Symfony 2, and if so where do I put this exception?
If this is no longer viable, how would I be able to test for an error 500?
You can do it like this.
//in your controller
$response = new Response();
$response->setStatusCode(500);
return $response;
Dont forget to add
use Symfony\Component\HttpFoundation\Response;
at the top of your file.
Edit : To force Symfony 500 error, your proposition is fine :
throw new \Exception('Something went wrong!');
Put it in a controller function.
You can do:
throw new Symfony\Component\HttpKernel\Exception\HttpException(500, "Some description");
The simplest way to do this is to:
return new Response('', 500);
Don't forget to include Symfony\Component\HttpFoundation\Response
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With