Skip to content

Instantly share code, notes, and snippets.

@einnar82
Last active August 14, 2020 00:37
Show Gist options
  • Save einnar82/f146455712209c84df5383e7d30adef5 to your computer and use it in GitHub Desktop.
Save einnar82/f146455712209c84df5383e7d30adef5 to your computer and use it in GitHub Desktop.
Proper exception handling in GuzzleHttp
<?php
namespace Foo\Bar;
use GuzzleHttp\Exception\RequestException;
use Guzzle\Http\Exception\ConnectException;
class Payment
{
public function request()
{
try {
$response = $client->request('GET', 'https://github.com/_abc_123_404');
$payload = json_decode($response->getBody(), true);
// This will return the original response
return $payload;
} catch (RequestException $exception) {
$payload = json_decode($exception->getResponse()->getBody(), true);
// This will return the actual exception
return $payload;
} catch (ConnectException $e) {
//Catch the guzzle connection errors over here.These errors are something
// like the connection failed or some other network error
return $e->getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment