处理 JSON API 响应的最佳方法是什么 returns 200 无论是否有错误

What is the best way to handle a JSON API response which returns 200 regardless of error or not

所以我正在向一些不可靠的来源(我无法控制)发出 Guzzle HTTP 请求。

响应 return 是 2 之一:

带有以下错误的 200 状态代码:

{
    "error": "Data source error, please try again"
}

和 200 状态代码,响应数据如下:

{
    "products": {
        "income": "Income Protection",
        "car": "Car Insurance"
    }
}

这是我通过 Guzzle 发出请求的方式:

        try {

            $client = new Client(array(
                'curl'   => array( CURLOPT_SSL_VERIFYPEER => false ),
                'verify' => false
            ));
            $res = $client->request($method, self::URI . $url, [
                $params
            ]);

            return $res->getBody();
        } catch (ClientException $e) {
            echo Psr7\str($e->getRequest());
            echo Psr7\str($e->getResponse());
        }

因此,当我收到上面的错误响应时,它永远不会到达代码的 catch 部分,因为它 return 是一个成功的 200 状态代码。

处理此问题的最佳方法是什么,以便我可以 return 适当地响应错误?

$result = json_decode($res);

if(!isset($result->error))...