Catching Guzzle exceptions
P粉511749537
P粉511749537 2023-11-16 14:36:03
0
12
1116

I'm trying to catch exceptions in a set of tests running on an API I'm developing, and I'm using Guzzle to consume API methods. I have wrapped the test in a try/catch block but it still throws an unhandled exception error. Adding an event listener as described in their documentation doesn't seem to do anything. I need to be able to retrieve responses with HTTP codes 500, 401, 400, in fact any response that is not 200, because if that doesn't work the system will set the most appropriate code based on the result of the call.

Current code example

foreach($tests as $test){

        $client = new Client($api_url);
        $client->getEventDispatcher()->addLi stener('request.error', function(Event $event) {        

            if ($event['response']->getStatusCode() == 401) {
                $newResponse = new Response($event['response']->getStatusCode());
                $event['response'] = $newResponse;
                $event->stopPropagation();
            }            
        });

        try {

            $client->setDefaultOption('query', $query_string);
            $request = $client->get($api_version . $test['method'], array(), isset($test['query'])?$test['query']:array());


          // Do something with Guzzle.
            $response = $request->send();   
            displayTest($request, $response);
        }
        catch (GuzzleHttpExceptionClientErrorResponseException $e) {

            $req = $e->getRequest();
            $resp =$e->getResponse();
            displayTest($req,$resp);
        }
        catch (GuzzleHttpExceptionServerErrorResponseException $e) {

            $req = $e->getRequest();
            $resp =$e->getResponse();
            displayTest($req,$resp);
        }
        catch (GuzzleHttpExceptionBadResponseException $e) {

            $req = $e->getRequest();
            $resp =$e->getResponse();
            displayTest($req,$resp);
        }
        catch( Exception $e){
            echo "AGH!";
        }

        unset($client);
        $client=null;

    }

Even with a specific catch block that throws the exception type, I still get a return

Fatal error: Uncaught exception 'GuzzleHttpExceptionClientErrorResponseException' with message 'Client error response [status code] 401 [reason phrase] Unauthorized [url]

As you might expect, all execution on the page stops. Adding the BadResponseException catch allows me to catch 404 correctly, but this doesn't seem to work with 500 or 401 responses. Can anyone suggest where I am going wrong.

P粉511749537
P粉511749537

reply all(12)
尊渡假赌尊渡假赌尊渡假赌

Three Major Transformations***Three Major Transformations of Wage Rice Cooker***Three Major Transformations of Wage Rice Cooker***Three Major Transformations of Wage Rice Cooker***Three Major Transformations of Wage Rice Cooker***Salary Three Major Transformations of Rice Cookers***Salary Rice Cooker

尊渡假赌尊渡假赌尊渡假赌

Afan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve

尊渡假赌尊渡假赌尊渡假赌

Afan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Ah Fan VS Ah the wage earner manual butterfly valve Butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve Ah Fan VS Ah wage earner manual butterfly valve

尊渡假赌尊渡假赌尊渡假赌

Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master

尊渡假赌尊渡假赌尊渡假赌

Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master

尊渡假赌尊渡假赌尊渡假赌

Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master

尊渡假赌尊渡假赌尊渡假赌

Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai, Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master Ai Master

尊渡假赌尊渡假赌尊渡假赌

Ah yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yeah Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes Yes, yes

P粉701491897

Depending on your project, you may need to disable guzzle exceptions. Sometimes encoding rules do not allow flow control exceptions. You can disable exceptions for Guzzle 3 as follows:

$client = new \Guzzle\Http\Client($httpBase, array(
  'request.options' => array(
     'exceptions' => false,
   )
));

This does not disable curl exceptions such as timeouts, but now you can easily get each status code:

$request = $client->get($uri);
$response = $request->send();
$statuscode = $response->getStatusCode();

To check if you got a valid code you can use something like this:

if ($statuscode > 300) {
  // Do some error handling
}

...or better yet handle all expected code:

if (200 === $statuscode) {
  // Do something
}
elseif (304 === $statuscode) {
  // Nothing to do
}
elseif (404 === $statuscode) {
  // Clean up DB or something like this
}
else {
  throw new MyException("Invalid response from api...");
}

Applies to Guzzle 5.3

$client = new \GuzzleHttp\Client(['defaults' => [ 'exceptions' => false ]] );

Thanks@mika

Guzzle 6

$client = new \GuzzleHttp\Client(['http_errors' => false]);
尊渡假赌尊渡假赌尊渡假赌

Ah yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yeah Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes Yes, yes

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!