5

In a Laravel project I need to call API REST to delete remote data.

My problem is that my catch statement dont catch Guzzle exception when I got an error. My code is the following :

try {
    $client = new \GuzzleHttp\Client();
    $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
    $status = $request->getStatusCode();
} catch (Exception $e) {
    var_dump($e);exit();
}

The exception is catched by Laravel but not in my catch statement. The exception throwed by Guzzle is :

GuzzleHttp\Ring\Exception\ConnectException

It raised in my line 3 of script and it doesn't catched in my script. Could you give me a way to catch the Guzzle Exception ?

I should indicate that I already seen these posts but I not get a good response : How to resolve cURL Error (7): couldn't connect to host? and Catching cURL errors from Guzzle

Community
  • 1
  • 1
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94

5 Answers5

14

It worked with me when I used

\GuzzleHttp\Exception\ConnectException

instead of

\Guzzle\Http\Exception\ConnectException

Muhammad
  • 444
  • 4
  • 9
4

I had a similar problem and solved it using the following thing.I have used your example and given the inline comments for you to understand.

try {
    $client = new \GuzzleHttp\Client();
    $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
    $status = $request->getStatusCode();
    if($status == 200){
        $response = $response->json();

    }else{
        // The server responded with some error. You can throw back your exception
        // to the calling function or decide to handle it here

        throw new \Exception('Failed');
    }

} catch (\Guzzle\Http\Exception\ConnectException $e) {
    //Catch the guzzle connection errors over here.These errors are something 
    // like the connection failed or some other network error

    $response = json_encode((string)$e->getResponse()->getBody());
}

Hope this helps!

Ymartin
  • 1,321
  • 1
  • 10
  • 24
  • 1
    $response = json_encode((string)$e->getResponse()->getBody()); doesn't work for me, use: $e->gethandlerContext()['error'] grabs the error message part: e.g. 'Could not resolve host: xxxxx' and use \GuzzleHttp\Exception\ConnectException, instead of catch (\Guzzle\Http\Exception\ConnectException $e) { – Homer Feb 19 '19 at 03:05
2

Maybe that exception is not extending the Exception class. You may try to catch it like:

try {
    $client = new \GuzzleHttp\Client();
    $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
    $status = $request->getStatusCode();
} catch (\GuzzleHttp\Ring\Exception\ConnectException $e) {
    var_dump($e);exit();
} catch (Exception $e) {
    // ...
}
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
1

You probably mean to catch \Exception (in the root namespace), either by adding the backslash in the catch statement or a use Exception statement.

Peter
  • 29,454
  • 5
  • 48
  • 60
0

Just Updating the answer to the new Guzzle exception namespace

try {
    $client = new \GuzzleHttp\Client();
    $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
    $status = $request->getStatusCode();
} catch (\GuzzleHttp\Exception\ConnectException $e) {
    var_dump($e);exit();
}