4

I am trying to create a three tier web application :

  1. Frontend (AngularJS)
  2. "API Exposure Layer" with Symfony2 and Guzzle (6) inspired by these slides by Meetic: http://www.slideshare.net/meeticTech/meetic-backend-mutation-with-symfony built with Symfony2, FOSRestBundle, and Guzzle 6 (8p/GuzzleBundle).
  3. API/Webservice

So I basically want :

  1. An AngularJS Frontend that calls my "API Exposure Layer"
  2. This "API Exposure Layer" calls my API/Webservice.
  3. API/Webservice persists data to the database and then sends an OK/Error to the "API Exposure Layer"
  4. "API Exposure Layer" relays the information to the frontend so that it can update/show errors if necessary

The problem I am facing is that Guzzle in my "API Exposure Layer" overrides any message coming from the API/Webservice with its own generic messages which are practically of no use to my frontend.

Example :

{
  "code": 500,
  "message": "Server error: 500"
}

instead of what is output by the API/Webservice

{
  "code":500,
  "message":"Token already exists"
}

My question is: How do I get the original API/Webservice messages from guzzle instead of the generic ones ?

Here is my "API/Webservice" controller method :

public function postAppTokensAction($guid)
{
    $request = $this->get('request');

    if (!$request->request->get('key')) {
        throw new HttpException(500, 'Missing Key');
    }

    if (!$request->request->get('secret')) {
        throw new HttpException(500, 'Missing Secret');
    }

    $repository = $this->getDoctrine()
        ->getRepository('AppBundle:App');

    $app = $repository->findOneByGuid($guid);

    $token = new Token();

    $token->setKey($request->request->get('key'));
    $token->setSecret($request->request->get('secret'));
    $token->setApp($app);

    try {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($token);
        $entityManager->flush();

        return $app;

    } catch(\Exception $e) {
        throw new HttpException(500, "Token allready exists");
    }  
}

Here is my "API Exposure Layer" Controller method :

public function postAppTokensAction($guid)
{
    $client   = $this->get('guzzle.client.ws_app');

    try { 
        $response = $client->request('POST', '/apps/' . $guid . '/tokens', [
            'json' => [
                'key' => '123',
                'secret' => '123'
            ]   
        ]);
    } catch (Exception $e) {
        //No luck in getting the original API message here
    }

    return json_decode($response->getBody(), true);
}

Edit :

I dont think that Catching exceptions from Guzzle is a duplicate because it's for an older version of Guzzle and the syntax has changed.

I have tried adding http_errors => false :

try { 
        $response = $client->request('POST', '/apps/' . $guid . '/tokens', [
            'http_errors' => false,
            'json' => [
                'key' => '12345555',
                'secret' => '123'
            ]   
        ]);
    } catch (\Exception $e) {
            die($e);
    }

    return json_decode($response->getBody(), true);

That just never sends an exception and the catch is skipped entirely.

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Purplefish32
  • 647
  • 1
  • 8
  • 24

1 Answers1

0

maybe this helps? Catching exceptions from Guzzle

Small excerpt:

$client = new \Guzzle\Http\Client($httpBase, array(
  'request.options' => array(
     'exceptions' => false,
   )
));
Community
  • 1
  • 1
mblaettermann
  • 1,916
  • 2
  • 17
  • 23
  • 1
    Thant link seems to be for older versions of Guzzle. I have tried the following : $response = $client->request('POST', '/apps/' . $guid . '/tokens', [ 'http_errors' => false, 'json' => [ 'key' => '12345555', 'secret' => '123' ] ]); But that just skips the catch entirely – Purplefish32 Nov 10 '15 at 15:04
  • Yeah, but you could then `->getReasonPhrase()` on the response? – mblaettermann Nov 10 '15 at 15:08
  • I changed to : $response = $client->request('POST', '/apps/' . $guid . '/tokens', [ 'http_errors' => false, 'json' => [ 'key' => '12345555', 'secret' => '123' ] ]); echo $response->getReasonPhrase(); But I am still getting the overidden "Internal Server Error" and not the original "Token allready exists" message – Purplefish32 Nov 10 '15 at 15:16