1

how can i avoid fatal error due to ?

 |GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host|

My code here do not catch it

try {
      $client = new Client();
      $response = $client->request('GET', $url);
      $content = json_decode($response->getBody(), true);     
    }
    catch (Exception $e) {
      \Drupal::logger('amu_hal')->error('this url aint good'.$url);
    }
4
  • Have you set the $url variable equal to something?
    – No Sssweat
    Feb 21, 2017 at 14:33
  • Is there more code than this? What is $url?
    – Kevin
    Feb 21, 2017 at 14:41
  • my question is not how to make the request correct but how to avoid a fatal error if the url is not correct.
    – Matoeil
    Feb 21, 2017 at 14:43
  • That is what I am asking, how is the $url formed? It is user input? Can you validate it with the Url class (create a Url object, then pass $url->toString())?
    – Kevin
    Feb 21, 2017 at 14:46

1 Answer 1

3

This is an example on how to catch this error:

    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\ClientException;
    use GuzzleHttp\Exception\RequestException;

    try {
         // example of status code 500
         $url='http://httpstat.us/500';
         $client = new Client();
         $response = $client->request('GET', $url);
         $content = json_decode($response->getBody(), true);     
    } catch (ClientException $e) {
    // catches all ClientExceptions
} catch (RequestException $e) {
    // catches all RequestExceptions
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.