Snippets
Laravel handle Guzzle Error
Hi,
If you have ever face such kinds of the issue where you are trying use getStatusCode()
is guzzle, however, you are always getting default laravel error page, then this post will help you.
In my case, I am using klaviyo api services to access. But when someone provides the wrong api key, I am getting the following errors-
GuzzleHttp\Exception\ClientException
Client error: `GET https://a.klaviyo.com/api/v2/lists?api_key=pk_068860eadc7b016aa81c312e203e82ac3c` resulted in a `403 FORBIDDEN` response: {"message":"The API key specified is invalid."}
Actually I don't have any clue why the getStatusCode()
doesn't trigger the error code instead of default and scary error code.
Then I figure it out a way by adding 'http_errors' => false
line in every request. So, here is my updated code-
$client->get('https://a.klaviyo.com/api/v2/lists?api_key=myWrongAPIKey', ['http_errors' => false]);
Now you will get expected getStatusCode()
even if it fails to connect with the server.
Hope it will work for you.
Thanks.