I am trying to create a three tier web application :
- Frontend (AngularJS)
- "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).
- API/Webservice
So I basically want :
- An AngularJS Frontend that calls my "API Exposure Layer"
- This "API Exposure Layer" calls my API/Webservice.
- API/Webservice persists data to the database and then sends an OK/Error to the "API Exposure Layer"
- "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.