1

For my Laravel application, I use the Goutte package to crawl DOMs, which allows me to use guzzle settings.

$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
    'timeout' => 15,
));
$goutteClient->setClient($guzzleClient);

$crawler = $goutteClient->request('GET', 'https://www.google.com/');

I'm currently using guzzle's timeout feature, which will return an error like this, for example, when the client times out:

cURL error 28: Operation timed out after 1009 milliseconds with 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

Now this is cool and all, but I don't actually want it to return a cURL error and stop my program.

I'd prefer something like this:

if (guzzle client timed out) {
    do this
} else {
    do that
}

How can I do this?

2 Answers 2

2

Use the inbuilt Laravel Exception class.

Solution:

<?php

namespace App\Http\Controllers;

use Exception;

class MyController extends Controller
{
    public function index()
    {
        try
        {
            $crawler = $goutteClient->request('GET', 'https://www.google.com');
        }
        catch(Exception $e)
        {
            logger()->error('Goutte client error ' . $e->getMessage());
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this works for me. Laravel HTTP client wrapped Guzzle and works well and handily but unfortunately it just does not catch connection failure without a server response. In some cases we do need to know if a thread is running or failed on the server therefore a ConnectionException should be regarded as one type of catch returns. For now guess we will have to write try { //try code } cat (Exception $long_useless_error) {//not succeeded, do something} to make our logic complete. p.s do not forget to quote use Exception; on the top of your controller.
1

Figured it out. Guzzle has its own error handling for requests.

Source: http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions

Solution:

use GuzzleHttp\Exception\RequestException;

...

try {
    $crawler = $goutteClient->request('GET', 'https://www.google.com');
    $crawlerError = false;
} catch (RequestException $e) {
    $crawlerError = true;
}


if ($crawlerError == true) {
    do the thing
} else {
   do the other thing
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.