Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Simulate 500 Error in Symfony 2

Tags:

symfony

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?

like image 555
Jon Avatar asked Jan 11 '13 18:01

Jon


3 Answers

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.

like image 91
Pierrickouw Avatar answered Nov 12 '22 14:11

Pierrickouw


You can do:

throw new Symfony\Component\HttpKernel\Exception\HttpException(500, "Some description");
like image 33
ButterDog Avatar answered Nov 12 '22 14:11

ButterDog


The simplest way to do this is to:

return new Response('', 500);

Don't forget to include Symfony\Component\HttpFoundation\Response.

like image 3
Jonathan Avatar answered Nov 12 '22 13:11

Jonathan