doude5860
2019-04-29 12:40
浏览 67
已采纳

Symfony 4:捕获异常

I am trying to catch errors but it doesn't work, I always see the Symfony Exception page,do you see any errors in my code? thanks in advance.

namespace App\Controller;
use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\AcceptHeader;
use Doctrine\ORM\ORMException;
use Doctrine\DBAL\DBALException;


public function index() {
        $request = Request::createFromGlobals();
        $content = $request->getContent();        
        $prod = [];
        $response = null;
        try
        {
            //$product->setPrice($request -> request -> get('price'));
            ....            
        }                  
        catch(\DBALException $e){
            $errorMessage = $e->getMessage();
            $response = New Response();
            $response -> setContent($errorMessage);
            $response -> setStatusCode(Response::HTTP_BAD_REQUEST);
        }
        catch(Exception $e){
            $errorMessage = $e->getMessage();
            $response = New Response();
            $response -> setContent($errorMessage);
            $response -> setStatusCode(Response::HTTP_BAD_REQUEST);

        }

        $response->headers->set('Content-Type', 'application/json');
        return $response;             
}

and this is the errors enter image description here thanks Andrea

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题