Monday, January 10, 2022

[FIXED] CakePHP 3 Http Client timeout error handling

Issue

I'm trying to use the CakePHP 3 Http Client to check urls for their response status code (404, 301, 200 etc)

$http = new Client();
$response = $http->get($links[$i]['url'],[],['timeout' => '10']);
$links[$i]['http_status'] = $response->statusCode();

However, if I come across a url that timesout, the entire script fails. I'm having trouble figuring out how to add error handling so that if it does timeout, I can log it and move on.

Any ideas?


Solution

The solution was to use try/catch. I had tried this initially but missed the \ before Exception on the catch

try {
    // code
} catch (\Exception $e) {
    // error
}


Answered By - Sanfly

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.