Skip to content
Advertisement

Prevent Guzzle from creating a 500 error on non 200 response

On my website I use Guzzle to report hacking attempts to AbuseIPDB. For example, when a hacker visits /.env a report will automatically get filed. AbuseIPDB gives a 429 when you send more than one report for the same IP. Guzzle then gives a 500 error as AbuseIPDB did not give a 200 OK.

My question is, how can I prevent Guzzle from killing the program when it gets a non 200 OK response? Is it possible to do this?

Advertisement

Answer

A GuzzleHttpExceptionServerException is thrown for 500 level errors if the http_errors request option is set to true. This exception extends from GuzzleHttpExceptionBadResponseException.

I will add a sample example to how to handle only 500 errors,

JavaScript

Similarly you can handle 400 exceptions using ClientException, see more about exceptions from docs.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement