nategood / httpful

A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
MIT License
1.74k stars 299 forks source link

How to handle connection refused as Httpful\Response #260

Open ghost opened 7 years ago

ghost commented 7 years ago

I have a HTTP_CLIENT class which is pretty thing - allows me to accept parameters and call the HTTPful library while doing some logging before and after the call, it's functions just return the raw response from \Httpful\Request::post and \Httpful\Request::get. It all works fine as long as the API I am calling is up, however if it has fallen over the below seems to happen.

$res = \Httpful\Request::post($uri)              
              ->body($body)
              ->sendsJSON()
              ->expectsJSON()
              ->send();

Results in an exception being thrown of type Httpful\Exception\ConnectionErrorException which I can catch no problem, but my issues comes when trying to return a useful value to the calling code.

I would like to be able to log the error and return a \Httpful\Response with a 500 error to the calling code. Is there an easy way to either just create a response object myself and return that, or in the POST and GET calls, have it not throw an exception but return the \Httpful\Response object (probably more elegant).

I've tried to create the \Httpful\Response object myself but can't seem to get it to construct.