search

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粉511749537380 days ago1181

reply all(12)I'll reply

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

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:15:06

    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

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

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:14:13

    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

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

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:13:10

    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

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

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

    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

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

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

    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

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

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:10:26

    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

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

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:08:15

    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

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

    尊渡假赌尊渡假赌尊渡假赌2023-11-17 16:06:56

    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
  • P粉701491897

    P粉7014918972023-11-17 14:08:04

    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]);

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

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

    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