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.
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;
}
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 exception from guzzle
you should set the guzzehttp client with option 'http_errors' => false,
the example code should be like this, document:guzzlehttp client http-error option explain
Set to false to disable throwing exceptions on an HTTP protocol errors (i.e., 4xx and 5xx responses). Exceptions are thrown by default when HTTP protocol errors are encountered.
$client->request('GET', '/status/500');
// Throws a GuzzleHttp\Exception\ServerException
$res = $client->request('GET', '/status/500', ['http_errors' => false]);
echo $res->getStatusCode();
// 500
$this->client = new Client([
'cookies' => true,
'headers' => $header_params,
'base_uri' => $this->base_url,
'http_errors' => false
]);
$response = $this->client->request('GET', '/');
if ($code = $response->getStatusCode() == 200) {
try {
// do danger dom things in here
}catch (/Exception $e){
//catch
}
}
Catch Guzzle exceptions on connections refused
Having tried your code, it seems to be throwing an instance of GuzzleHttp\Exception\ConnectException
, so change the catch to
} catch (ConnectException $e) {
$response['status'] = 'Connect Exception';
$response['message'] = $e->getMessage();
}
( Adding the appropriate use statement...
use GuzzleHttp\Exception\ConnectException;
)
Also noticed that it doesn't have $e->getResponse()->getStatusCode()
, which is why I've just set it to a fixed string.
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
Storing and Displaying Unicode String (हिन्दी) Using PHP and MySQL
JavaScript: How to Create Jsonp
Get First 100 Characters from String, Respecting Full Words
Twitter API Returns Error 215, Bad Authentication Data
Generating Facebook Open Graph Meta Tags Dynamically
PHP Read Sub-Directories and Loop Through Files How To
How to Clear Browser Cache With PHP
Catching Exceptions from Guzzle
How to Handle Ipv6 Addresses in PHP
Localhost Vs. 127.0.0.1 in MySQL_Connect()
How to Embed a .Png Image into an HTML Page
Best Way to Automatically Remove Comments from PHP Code
Alternative to MySQL_Real_Escape_String Without Connecting to Db