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

Do we really have to catch Guzzle RequestException in getResponse? #70

Closed
frzsombor opened this issue Oct 18, 2016 · 6 comments
Closed

Comments

@frzsombor
Copy link
Contributor

frzsombor commented Oct 18, 2016

Hi!

I'm working on a project that uses this library, and I noticed that in the getResponse function, there is a try-catch block (line 288-295) around the Guzzle request, which just re-throws the error message. The problem is that, this way I can't really build a good error handling around the translation process, because I can't tell the difference between ConnectException, ClientException, ServerException, etc., which would be great in my opinion.

Do we really have to catch Guzzle RequestExceptions in getResponse?
Can you tell me, what is the purpose of catching and re-throwing the Exceptions there?

@Stichoza
Copy link
Owner

Ok, I got the point. The reason was to be more independent from http client package. I mean if someday we will switch from Guzzle to something else, it would be smooth operation without major changes and developers will not have to change their code to handle SomeNewHttpClientExceptions instead of GuzzleRequestException.

@frzsombor
Copy link
Contributor Author

Thanks for the explanation, I understand now.
Here are the exceptions of Guzzle and how they extends other classes:

\Exception
    \ErrorException (this library)
    \RuntimeException
        SeekException
        TransferException
            RequestException
                ConnectException
                TooManyRedirectsException
                BadResponseException
                    ClientException
                    ServerException

In my situation it is important to check, if the exception is a ConnectException or any BadResponseException, because I'm using proxies to get around Google IP bans (as I even get banned with this library after a few hours - maybe due to large number of requests). If a proxy is temporary down for some reason (which happens many times), I get a ConnectException, but if Google banned any of them (and I shouldn't use it for like 24 hours), it will raise a ClientException (4xx type errors, like 403 Forbidden) or ServerException (5xx type errors, like 503 Service Unavailable).

• I think it would be wrong to switch to Guzzle's exceptions, because
the parent class of Guzzle's exceptions is \RuntimeException, and this library throws \ErrorException, so if someone only catches the second one, an update could break his site/app.

• I think it would be good to switch to Guzzle's exceptions, because
As long as someone catches \Exception, the using of Guzzle's custom exceptions shouldn't cause any problem. And I think catching custom exceptions, but not catching \Exception is a bad programming practice, so hopefully nobody using this library made this mistake.

Of course it's on you to switch or not, I can use my synchronized private fork anytime, just wanted to share my thoughts and ideas :)

@Stichoza
Copy link
Owner

Sure, thanks for sharing your opinion, appreciated that.
I'll think about this again, because currently both options seem to be correct. Can't decide easily.

@Stichoza
Copy link
Owner

Stichoza commented Dec 4, 2018

screen shot 2018-12-04 at 4 22 03 am

Leaving ErrorException after two years of thinking 😆 For the same reasons stated above.

@Stichoza Stichoza closed this as completed Dec 4, 2018
@Stichoza
Copy link
Owner

Stichoza commented Nov 29, 2022

This issue is about to receive a good update in v5.1.0 🎉

@frzsombor
Copy link
Contributor Author

Great job, props to you for still maintaining this repo and taking care of the feedbacks!

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