How to Simulate 500 Error in Symfony 2

前端 未结 6 747
闹比i
闹比i 2021-02-07 00:25

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

相关标签:
6条回答
  • 2021-02-07 01:14

    A good way to do it can be:

    use Symfony\Component\HttpKernel\Exception\HttpException;
    
    throw new HttpException(\Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR, 'Testing the 500 error');
    
    0 讨论(0)
  • 2021-02-07 01:15

    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.

    0 讨论(0)
  • 2021-02-07 01:18

    You can do:

    throw new Symfony\Component\HttpKernel\Exception\HttpException(500, "Some description");
    
    0 讨论(0)
  • 2021-02-07 01:24

    The simplest way to do this is to:

    return new Response('', 500);

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

    0 讨论(0)
  • 2021-02-07 01:24

    If you want to trigger a FatalErrorException in Symfony2 to see if you app is handling it correctly, you can create an action like this in your controller:

    public function fatalErrorExceptionAction()
    {
        $unknown->getVoid();
    }
    

    The division by zero will generate a Warning while throwing the Exception, well, it'll just throw it. :-)

    0 讨论(0)
  • 2021-02-07 01:24

    You have to use Symfony\Component\HttpKernel\Exception\HttpException.

    Another way should be doing something like 1/0;, but I haven't tested it.

    0 讨论(0)
提交回复
热议问题