Home  >  Q&A  >  body text

Catching Guzzle exceptions

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粉511749537P粉511749537361 days ago1140

reply all(12)I'll reply

  • P粉598140294

    P粉5981402942023-11-17 13:38:05

    If an exception is thrown within this try block, then in the worst case Exception any uncaught should be caught.

    Consider the first part of the test is to throw an exception and wrap it in a try block.

    reply
    0
  • 尊渡假赌尊渡假赌尊渡假赌

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:05:11

    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

    reply
    0
  • Cancelreply