Last active
August 14, 2020 00:37
-
-
Save einnar82/f146455712209c84df5383e7d30adef5 to your computer and use it in GitHub Desktop.
Proper exception handling in GuzzleHttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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