2016-02-17 56 views
2

我嘗試在try的catch部分引入錯誤500頭。我通過Ajax在symfony控制器方法中調用我的API方法。我的問題是,我無法顯示,因爲這個symfony的警告頁面如何跳過symfony2警告消息

enter image description here

這裏的API端代碼的錯誤消息:

try{ 
      //Some code 

     }catch (Exception $e){ 
      header('HTTP/1.1 500 Internal Server Error'); 
      die($e->getMessage()); // I get this message in the error part of the ajax call 
     } 

控制器的方法:

public function createModelAction(){ 

    $mail = "test"; 
    $pass = "test"; 

    $oauth = new OAuth($mail, $pass); 

    $baseUrl = $this->container->getParameter('url_api'); 

    // Récupération des rapports d'avis du réseau demandé 
    $link = "{$baseUrl}model/" . $mail . "/" . $pass . "/test"; 

    $oauth->fetch($link, $_REQUEST, OAUTH_HTTP_METHOD_POST); 
    $response = json_decode($oauth->getLastResponse(), true); 

    if ($response['error']) { 
     return new JsonResponse($response['error']); 
    } else { 
     return new JsonResponse($response['model']); 
    } 
} 

所以任何替代方案都可以跳過這個問題

感謝

+0

你可以顯示你的控制器嗎? –

回答

2

使用Symfony的請求和響應,而不是手動使用header

更改您的API代碼:

} catch (Exception $e) { 
    $response['error'] = $e->getMessage(); 

    return $response; 
} 

在你的控制,定義響應的狀態代碼:

if ($response['error']) { 
    return new JsonResponse($response['error'], 500); 
} 

您還可以查看創建ExceptionListener並返回一個JsonResponse而不是拋出異常。