【问题标题】:How to not override original guzzle message如何不覆盖原始 guzzle 消息
【发布时间】:2016-02-11 09:53:35
【问题描述】:

我正在尝试创建一个三层网络应用程序:

  1. 前端 (AngularJS)
  2. “API 暴露层”与 Symfony2 和 Guzzle (6) 的灵感来自 Meetic 的这些幻灯片:http://www.slideshare.net/meeticTech/meetic-backend-mutation-with-symfony 使用 Symfony2、FOSRestBundle 和 Guzzle 6 (8p/GuzzleBundle) 构建。
  3. API/网络服务

所以我基本上想要:

  1. 一个调用我的“API 暴露层”的 AngularJS 前端
  2. 这个“API 暴露层”调用我的 API/Web 服务。
  3. API/Webservice 将数据持久化到数据库中,然后将 OK/Error 发送到“API 暴露层”
  4. “API 暴露层”将信息中继到前端,以便在必要时更新/显示错误

我面临的问题是,我的“API 暴露层”中的 Guzzle 使用自己的通用消息覆盖来自 API/Web 服务的任何消息,这些消息对我的前端几乎没有用处。

例子:

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

而不是 API/Web 服务输出的内容

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

我的问题是:如何从 guzzle 获取原始 API/Webservice 消息而不是通用消息?

这是我的“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 => 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);

这只是从不发送异常,并且完全跳过了捕获。

【问题讨论】:

  • 你是如何实例化 GuzzleHttp\Client 和 GuzzleHttp\HandlerStack 的?
  • 我刚刚找到了解决方案,它与 guzzle 完全无关,我的 API 没有发送正确的信息。对不起!

标签: symfony fosrestbundle guzzle6


【解决方案1】:

也许这有帮助? Catching exceptions from Guzzle

小摘录:

$client = new \Guzzle\Http\Client($httpBase, array(
  'request.options' => array(
     'exceptions' => false,
   )
));

【讨论】:

  • 该链接似乎适用于旧版本的 Guzzle。我尝试了以下方法: $response = $client->request('POST', '/apps/' . $guid . '/tokens', [ 'http_errors' => false, 'json' => [ 'key' => '12345555', '秘密' => '123' ] ]);但这只是完全跳过了问题
  • 是的,但是您可以在回复中->getReasonPhrase() 吗?
  • 我改为: $response = $client->request('POST', '/apps/' . $guid . '/tokens', [ 'http_errors' => false, 'json' => [ 'key' => '12345555', '秘密' => '123' ] ]);回声 $response->getReasonPhrase();但我仍然收到覆盖的“内部服务器错误”,而不是原始的“令牌已经存在”消息
猜你喜欢
  • 2015-12-27
  • 2015-02-03
  • 1970-01-01
  • 2017-12-25
  • 2011-04-28
  • 2014-09-15
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多