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.
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
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:
This does not disable curl exceptions such as timeouts, but now you can easily get each status code:
To check if you got a valid code you can use something like this:
...or better yet handle all expected code:
Applies to Guzzle 5.3
Thanks@mika
Guzzle 6
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