Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取access_toekn失败之后抛出异常的地方,能够与其他地方统一使用下述这个 resolveResponse 返回数据 #951

Closed
Sky9th opened this issue Oct 12, 2017 · 1 comment

Comments

@Sky9th
Copy link

Sky9th commented Oct 12, 2017

我用的环境

CentOS7.3 APACHE2.4
PHP 版本:7.0
overtrue/wechat 版本:4.0-devmaster
是否使用了框架?框架名称:TP5.0

问题及现象

overtrue\wechat\src\Kernel\AccessToken.php ---- line144

public function requestToken(array $credentials): array
    {
        $response = $this->sendRequest($credentials);
        $result = json_decode($response->getBody()->getContents(), true);

        if (empty($result[$this->tokenKey])) {
            throw new HttpException('Request access_token fail: '.json_encode($result, JSON_UNESCAPED_UNICODE), $response);
        }

        return $result;
}

希望这个获取access_toekn失败之后抛出异常的地方,能够与其他地方统一使用下述这个 resolveResponse 返回数据

protected function resolveResponse(ResponseInterface $response, string $type)
    {
        $response = Response::buildFromPsrResponse($response);

        switch ($type) {
            case 'collection':
                return $response->toCollection();
            case 'array':
                return $response->toArray();
            case 'object':
                return $response->toObject();
            case 'raw':
            default:
                $response->getBody()->rewind();
                if (class_exists($type)) {
                    return new $type($response);
                }

                return $response;
        }
    }

overtrue added a commit that referenced this issue Oct 22, 2017
@overtrue
Copy link
Collaborator

@Sky9th

try {
    $token->requestToken($credentials);
} catch (\Exception $e) {
     $e->response; // \Psr\Http\Message\ResponseInterface|null
     $e->formattedResponse; //根据 response_type 格式化的结果
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants