Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] cURL \Web->request() should return errorCode too #318

Open
exodus4d opened this issue Dec 28, 2018 · 2 comments
Open

[Feature] cURL \Web->request() should return errorCode too #318

exodus4d opened this issue Dec 28, 2018 · 2 comments

Comments

@exodus4d
Copy link

Issue:

In case a cURL requests failed, \Web::instance()->request($url, $options) returns a string representation of the error in the returned Array.
This makes it hard to handle different error types. I suggest adding a new errorCode to the returned Array.

Suggestion:

Add new errorCode to returned Array. Example shows a request that timedOut :

[
  'body' => '',
  'headers' => [],
  'engine' => 'cURL',
  'cached' => false,
  'error' => 'Connection timed out after 16 milliseconds',
  'errorCode' => 28    // <== new entry
]

Implementation (suggestion):

Replace this code https://github.com/bcosca/fatfree/blob/master/lib/web.php#L303

ob_start();
curl_exec($curl);
$err=curl_error($curl);
curl_close($curl);
$body=ob_get_clean();

... with something like this:

ob_start();
curl_exec($curl);
if($errCode = curl_errno($curl)){  // e.g. 28 - Must be done before curl_close(), returns 0||null if no error found
    $errMessage = curl_strerror($errCode ); // e.g. "Timeout was reached"
    $err = curl_error($curl) // e.g. "Connection timed out after 16 milliseconds"
}
curl_close($curl);
$body=ob_get_clean();

... and add $errCode (maybe $errMessage as well) to the response Array.
As you can see, the Strings are slightly different.

Test:

Either set $options['timeout'] = 1 as config option for a valid request and hope for a timeout,
or add this to the _curl($url,$options) method and set the timeout to e.g. 10ms:

curl_setopt($curl,CURLOPT_CONNECTTIMEOUT_MS, 10);
curl_setopt($curl,CURLOPT_TIMEOUT_MS, 10);
@joseffb-mla
Copy link

hmm maybe an error array instead of two vars?


[
  'body' => '',
  'headers' => [],
  'engine' => 'cURL',
  'cached' => false,
  'error' => 'Connection timed out after 16 milliseconds',
  'errors' => {[  //new name to keep backward compatible.
     [
     'message' =>'Connection timed out after 16 milliseconds',
     'code' => 28  
     ], //array in case there are more then one error
]}
]

@exodus4d
Copy link
Author

Return an array with errors (and errorCode) can also work. But I´m not sure if request can fail with multiple errors.

We should also think about _stream() and _socket() return Array. All 3 methods return the same result Array.

Anyway it would useful to have the code and work with them like:

$response = \Web::instance()->request($url, $options);
switch($response['errorCode']){
    case CURLE_URL_MALFORMAT:
        $f3->error(500, 'WTF!');
        break;
    case CURLE_OPERATION_TIMEOUTED:
        // no error here, maybe try to resend same request again up to 3 times...
        break;
}

@ikkez ikkez transferred this issue from bcosca/fatfree Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants