2016-01-18 109 views
1

我使用FOERestBundle和類View。當我確認實體我有這樣的對象錯誤,這就是:FOSRestBundle返回對象錯誤

[ 
{ 
    "property_path": "main_skill", 
    "message": "This value should not be blank." 
}, 
{ 
    "property_path": "type", 
    "message": "This value should not be blank." 
}, 
{ 
    "property_path": "description", 
    "message": "This value should not be blank." 
} 
] 

我需要返回的對象錯誤,當這樣的

[ 
{ 
    "property_path": "main_skill", 
    "message": "This value should not be blank." 
}, 
] 

用戶不是有效的安全令牌現在我有純文本。這是我的終點

/** 
* Update existing Bit from the submitted data. 
* 
* @ApiDoc(
* resource = true, 
* description = "Update single Bit", 
* parameters={ 
*  {"name"="status", "dataType"="string", "required"=false, "description"="status for bit"}, 
*  {"name"="text", "dataType"="string", "required"=true, "description"="text for rejected"}, 
*  {"name"="token", "dataType"="string", "required"=true, "description"="is equally md5('email'.secret_word)"} 
* }, 
* statusCodes = { 
*  200 = "Bit successful update", 
*  400 = "Secret token is not valid" 
* }, 
* section="Bit" 
*) 
* @RestView() 
* 
* @param Request $request 
* @param string $id 
* 
* @return View 
*/ 
public function putBitAction(Request $request, $id) 
{ 
    $manager = $this->getDoctrine()->getManager(); 
    $token = $this->get('request')->request->get('token'); 
    $user = $this->getDoctrine()->getRepository('MyBundle:Users')->findOneBySecuritytoken($token); 
    $bit = $manager->getRepository('MyBundle:Bit')->find($id); 
    $view = View::create(); 

    if (!empty($user) && !empty($bit) && !empty($token)) { 

      *some logic 
      $view = $this->view($bit, 200); 

      return $this->handleView($view); 
     } 
    } else { 
     $view = $this->view('Secret token is not valid', 400); 

     return $this->handleView($view); 
    } 
} 

現在我有明文

Response Body [Raw] 
"Secret token is not valid" 

這是返回的對象錯誤驗證,這是確定的

[ 
{ 
    "property_path": "main_skill", 
    "message": "This value should not be blank." 
}, 
{ 
    "property_path": "type", 
    "message": "This value should not be blank." 
}, 
{ 
    "property_path": "description", 
    "message": "This value should not be blank." 
} 
] 

如何返回的自定義錯誤,如對象不是純文本?

回答

1

只是通過你的數據,如一個數組,並告訴視圖呈現爲JSON應該產生一個輸出像你想

$view = $this->view(
       array(
       'property_path' => 'main_skill', 
       'message' => "error" 
       //whatever your object/array structure is 
      ), 
       500 //error code for the error 
      ); 

$view->setFormat('json');  
return $this->handleView($view); 
0

可以使用的Symfony的HTTPExceptions因爲這些將被FOSRestBundle處理。

請參見:http://symfony.com/doc/current/bundles/FOSRestBundle/4-exception-controller-support.html

public function putBitAction(Request $request, $id) 
{ 
    $token = $request->get('token'); 
    if (null === $token) { 
     throw new BadRequestHttpException('Provide a secret token'); 
    } 

    $manager = $this->getDoctrine()->getManager(); 
    $user = $manager->getRepository('MyBundle:Users')->findOneBySecuritytoken($token); 
    if (null === $user) { 
     throw new BadRequestHttpException('Secret token is not valid'); 
    }   

    $bit = $manager->getRepository('MyBundle:Bit')->find($id); 
    if (null === $token) { 
     throw new NotFoundHttpException('Bid not found'); 
    } 

    $view = $this->view($bit, 200); 
    return $this->handleView($view); 
} 

這是怎樣一個PUT要求?您應該重命名爲getBidAction

+0

我已經把邏輯。我有一個prod枝: exception_controller:ArtelProfileBundle:Exception:showException this action = return $ this-> redirect($ this-> generateUrl('login_route'));和BadRequestHttpException,NotFoundHttpException不會在prod中返回prod返回login_page,而是查看prod工作得很好 –