2017-05-31 42 views
0

我通過Guzzle發送帖子,並且有些產品正在返回InvalidArgumentException錯誤,我如何獲取此數據?不幸的是,在文檔中沒有關於這一點。如何使用Guzzle獲取InvalidArgumentException?

這是錯誤

InvalidArgumentException in functions.php line 324: 
json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded 

我的陷阱:

catch (ClientException $e) { 

       if ($e->hasResponse()) { 
        $resposta = Psr7\str($e->getResponse()); 
        $incrementer->cron_value = $incrementer->cron_value + 1; 
        $incrementer->save(); 
        return view('home'); 
       } 
      } 
      catch (RequestException $e) { 
       return view('home'); 
       if ($e->hasResponse()) { 
        $incrementer->cron_value = $incrementer->cron_value + 1; 
        $incrementer->save(); 
        return view('home'); 
       } 
      } 
+0

總是發送您的請求在嘗試..catch塊,例如try {//你的guzzle動作} catch(Exception $ e){echo $ e-> getMessage(); }' –

+0

是的,我用try catch,但是我沒有看到任何方法在Guzzle文檔中得到這種錯誤。例如,我使用ClientException和RequestException,但由於錯誤是InvalidArgumentException,所以它不會通過try catch過濾 –

+0

如果正確執行,則可以捕獲所有異常。向我們展示你的'try' /'catch'。 – ceejayoz

回答

1

正如你可以狂飲的代碼中看到,有在這條線的一般例外(從根命名空間)。

if (JSON_ERROR_NONE !== json_last_error()) { 
    throw new \InvalidArgumentException(
     'json_encode error: ' . json_last_error_msg()); 
} 

因此,除了其他例外情況外,您還必須瞭解它。

try { 
    //... 
} catch (ClientException $e) { 
    // ... 
} catch (RequestException $e) { 
    // ... 
} catch (\InvalidArgumentException $e) { 
    // Your UTF error. 
} 
+0

完美!謝謝! –