Handle Guzzle exception and get HTTP body
Guzzle 3.x
Per the docs, you can catch the appropriate exception type (ClientErrorResponseException
for 4xx errors) and call its getResponse()
method to get the response object, then call getBody()
on that:
use Guzzle\Http\Exception\ClientErrorResponseException;
...
try {
$response = $request->send();
} catch (ClientErrorResponseException $exception) {
$responseBody = $exception->getResponse()->getBody(true);
}
Passing true
to the getBody
function indicates that you want to get the response body as a string. Otherwise you will get it as instance of class Guzzle\Http\EntityBody
.
Catch Guzzle Exception and return string
Try to use exception hierarchy to catch any exception. ClientException
only catches status code between 400x-499. To catch other exception or catch within the same Exception you can use RequestException
.
public static function get_list($list_id)
{
$lists = self::get_lists();
$params = [
'list.fields' => 'created_at,follower_count,member_count,private,description,owner_id',
'user.fields' => 'created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld'
];
try {
$list = $lists->get($list_id, $params);
if($list->getStatusCode() == 200)){
$return_list = json_decode($list->getBody(),true);
}
} catch (\GuzzleHttp\Exception\ClientException $e) {
$error['error'] = $e->getMessage();
$error['request'] = $e->getRequest();
if($e->hasResponse()){
// you can pass a specific status code to catch a particular error here I have catched 400 Bad Request.
if ($e->getResponse()->getStatusCode() == '400'){
$error['response'] = $e->getResponse();
}
}
return $error;
} catch(\GuzzleHttp\Exception\RequestException $se){
$error['error'] = $e->getMessage();
$error['request'] = $e->getRequest();
return $error;
} catch(Exception $e){
//other errors
}
return $list;
}
Catching exceptions from Guzzle
If the Exception is being thrown in that try
block then at worst case scenario Exception
should be catching anything uncaught.
Consider that the first part of the test is throwing the Exception and wrap that in the try
block as well.
How to Retrieve Form Parameters sent on a Guzzle Exception?
The form encoded parameters can be found on Request Body.
try {
$reponse = $this->client->post($uri, [
'form_params' => $params,
'headers' => $this->getHeaders()
]);
} catch (RequestException $e){
echo (string) $e->getRequest()->getBody();
}
Guzzle: Can't catch exception details
The exception should be an instance of BadResponseException which has a getResponse method. You can then cast the response body to a string.
$response = json_decode($ex->getResponse()->getBody()->getContents(), true);
Related Topics
How to Pass an Array of Pdo Parameters Yet Still Specify Their Types
Loop Through All Months in a Date Range
How to Perform Multiple Guzzle Requests at the Same Time
How to Ipc Between PHP Clients and a C Daemon Server
How to Generate a Custom Menu/Sub-Menu System Using Wp_Get_Nav_Menu_Items in Wordpress
Installing Pdo Driver on MySQL Linux Server
How to Save PHP Session Data to a Database Instead of in the File System
How to Document Magic (_Call and _Callstatic) Methods for Ides
Browscap Ini Directive Not Set
Process Very Big CSV File Without Timeout and Memory Error
Is There Java Hashmap Equivalent in PHP
PHP Warning: Unknown: Failed to Open Stream