我正在尝试创建一个三层Web应用程序:
所以我基本上想要:
我面临的问题是我的" API Exposure Layer"覆盖来自API / Webservice的任何消息及其自己的通用消息,这些消息对我的前端几乎毫无用处。
示例:
{
"code": 500,
"message": "Server error: 500"
}
而不是API / Webservice输出的内容
{
"code":500,
"message":"Token already exists"
}
我的问题是:如何从guzzle获取原始API / Web服务消息而不是通用消息?
这是我的" API / Webservice"控制器方法:
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");
}
}
这是我的" API曝光层"控制器方法:
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);
}
编辑:
我不认为Catching exceptions from Guzzle是重复的,因为它适用于较旧版本的Guzzle并且语法已经改变。
我尝试过添加http_errors =>错误:
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);
这永远不会发送异常,并且完全跳过了捕获。
答案 0 :(得分:0)
也许这有帮助吗? Catching exceptions from Guzzle
小摘录:
$client = new \Guzzle\Http\Client($httpBase, array(
'request.options' => array(
'exceptions' => false,
)
));