php - Catch exception from guzzle -


i'm using laravel , have setup abstract class method response various apis i'm calling. if api url unreachable throws exception. know i'm missing something. great me.

$offers = [];     try {       $appurl = parse_url($this->apiurl);        // call api using guzzle       $client = new client('' . $appurl['scheme'] . '://' . $appurl['host'] . '' . $appurl['path']);        if ($appurl['scheme'] == 'https') //if https disable ssl certificate         $client->setdefaultoption('verify', false);        $request = $client->get('?' . $appurl['query']);       $response = $request->send();       if ($response->getstatuscode() == 200) {         $offers = json_decode($response->getbody(), true);       }     } catch (clienterrorresponseexception $e) {       log::info("client error :" . $e->getresponse()->getbody(true));     } catch (servererrorresponseexception $e) {       log::info("server error" . $e->getresponse()->getbody(true));     } catch (badresponseexception $e) {       log::info("badresponse error" . $e->getresponse()->getbody(true));     } catch (\exception $e) {       log::info("err" . $e->getmessage());     }      return $offers; 

you should set guzzehttp client option 'http_errors' => false, example code should this, document:guzzlehttp client http-error option explain

set false disable throwing exceptions on http protocol errors (i.e., 4xx , 5xx responses). exceptions thrown default when http protocol errors encountered.

$client->request('get', '/status/500'); // throws 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 {        // danger dom things in here    }catch (/exception $e){       //catch    }  } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -