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

Improve error control #403

Open
gotardo opened this issue Oct 26, 2016 · 8 comments
Open

Improve error control #403

gotardo opened this issue Oct 26, 2016 · 8 comments

Comments

@gotardo
Copy link

gotardo commented Oct 26, 2016

I am missing a better error control on Curl class. May be, having the posibility to decorate the class with some methods to control errors and throw the needed exceptions. That would not change the class behaviour and would add a good feature.
Do you think it could be interesting for the project?

@zachborboa
Copy link
Member

zachborboa commented Oct 28, 2016

Right now we have

$curl = new Curl();
$curl->get($url, $data);
if ($curl->error) {
    // Handle error.
} else {
    // Handle response.
}

What would you like to be able to do?

@gotardo
Copy link
Author

gotardo commented Oct 28, 2016

Hi,
I'd like to be able to have a better control con the error with something like this:

$curl = new Curl();
try {
    $curl->get($url, $data);
}
catch (CurlException $e) {
    MyErrorLogger::log($e->getMessage())
}

It means that the logic to read the error message should be inside the ::exec()method. I think that the logic of the error control would be easier to reuse this way.

@zachborboa
Copy link
Member

Try/Catch For:

  • Keeps error-handling code separate
  • Handling different error types
  • Cascading exceptions
  • Handle errors with user-defined error handler (set_error_handler())

@zachborboa
Copy link
Member

Try/Catch Against:

  • Slower (but does not add significant overhead)
  • Backwards compatible

@zachborboa
Copy link
Member

@zachborboa
Copy link
Member

A backwards-compatible version could look like this:

$curl = new Curl();
$curl->get($url, $data);
try {
    $curl->catch();
} catch (CurlException $e) {
    // Handle error.
    MyErrorLogger::log($e->getMessage())
}

@gvlasov
Copy link

gvlasov commented May 28, 2023

This would be really useful if there was an optional shorthand like the existing ->verbose() to turn any CURL error into an exception that contains all the information about the error, that would speed up debugging a lot. Like:

$curl = new Curl();
$curl->fragile();
$curl->get("http://never-resolve"); // throws immediately and tells what happened

I think this is a highly reusable scenario, because when debugging we rarely care to handle CURL errors, just need to know what error happened.

@zachborboa
Copy link
Member

This would be really useful if there was an optional shorthand like the existing ->verbose() to turn any CURL error into an exception that contains all the information about the error, that would speed up debugging a lot.

One solution would be to implement something like Curl::raiseErrors() which would turn errors into exceptions:

$curl = new Curl();
$curl->raiseErrors();
try {
    $curl->get($url, $data);
} catch (CurlException $e) {
    // Handle error.
}

However, exceptions wouldn't ever be raised if $curl->raiseErrors() was mistakenly missing:

$curl = new Curl();
try {
    $curl->get($url, $data);
} catch (CurlException $e) {
    // Exception never raised because $curl->raiseErrors() wasn't called.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants