3
   function order_confirmationAction($order,$token) { 

        $client = new \GuzzleHttp\Client();
        $answer  = $client->post("http://www.fullcommerce.com/rest/public/Qtyresponse",
                    array('body' => $order)
        );

        $answer  = json_decode($answer); 

        if ($answer->status=="ACK") {
            return $this->render('AcmeDapiBundle:Orders:ack.html.twig', array(
            'message'   => $answer->message,
        ));
        } else throw new \Symfony\Component\HttpKernel\Exception\HttpException(500, $answer->message);
}

If $client->post() response status code is an "Error 500" Symfony stops the script execution and throw new exception before the json decoding. How can I force Symfony to ignore $client->post() bad response and execute till the last if statement?

2 Answers2

3
            $client = new \GuzzleHttp\Client();
            try {
                $answer  = $client->post("http://www.fullcommerce.com/rest/public/Qtyresponse",
                        array('body' => $serialized_order)
                );
            }
            catch (\GuzzleHttp\Exception\ServerException $e) {

                if ($e->hasResponse()) {
                    $m = $e->getResponse()->json();
                    throw new \Symfony\Component\HttpKernel\Exception\HttpException(500, $m['result']['message']);
                }

            }

I solved like this. In that way I can access to responses of remote server even if it returns an error 500 code.

0

Per Guzzle documentation:

Guzzle throws exceptions for errors that occur during a transfer.

Specifically, if the API responds with a 500 HTTP error, you shouldn't expect its content to be JSON, and you don't want to parse it, so you're better off re-throwing an exception from there already (or informing the user that something went wrong). I would suggest trying this out:

function order_confirmationAction($order, $token) { 
    $client = new \GuzzleHttp\Client();
    try {
        $answer  = $client->post("http://www.fullcommerce.com/rest/public/Qtyresponse",
            array('body' => $order)
        );
    }
    catch (Exception $e) {
        throw new \Symfony\Component\HttpKernel\Exception\HttpException(500, $e->getMessage());
    }

    $answer  = json_decode($answer); 

    if ($answer->status=="ACK") {
        return $this->render('AcmeDapiBundle:Orders:ack.html.twig', array(
            'message'   => $answer->message,
        ));
    } else {
        throw new \Symfony\Component\HttpKernel\Exception\HttpException(500, $answer->message);
    }
}

It is probably also a good idea to check for errors when JSON-decoding the response, because there could be surprises in the content you're getting (eg. wrong format, missing or unexpected fields or values, etc.).

Eric Redon
  • 1,712
  • 12
  • 21
  • 1
    I think you are right when you choose the double thorwing, as the two type of check require. But in my specific case $client->post() return a json even if the status code is "500". – EmaOnTheBlock Jul 30 '14 at 22:12
  • 2
    indeed the json contain the message that explain the error so i need to bypass any guzzle client exception! :( – EmaOnTheBlock Jul 30 '14 at 22:15