php-http / curl-client

cURL client
http://httplug.io
MIT License
445 stars 28 forks source link

Response body not available on 4XX and 5XX errors when using ErrorPlugin #28

Closed tuupola closed 8 years ago

tuupola commented 8 years ago

When consuming API's the response body ofter includes additional information on 4XX and 5XX errors. Currently both sendRequest() and wait() throw RequestException which does not allow accessing the response. This would be resolved by throwing HttpException instead.

Although I am not sure if sendRequest() actually ever throws RequestException even though code is there. Documentation says clients always return response and ErrorPlugin should be used to throw exceptions.

Different clients seem to be behaving differently. For example React adapter returns response with 404 status instead of throwing.

https://gist.github.com/tuupola/9862b11cc8fb6f7131545fffea900ee5

tuupola commented 8 years ago

I am bit confused at the moment. Currently Curl and Guzzle clients behave in differently from React and Socket clients. With 4XX and 5XX responses call to $promise->wait() with Curl and Guzzle clients throw an exception. React and Socket clients however do call $promise->then(...). See the gist above.

Which behaviour is considered correct?

mekras commented 8 years ago

After fixing #27 and your gist, new results are:

Guzzle async exception: Http\Client\Exception\HttpException
Guzzle sync exception: Http\Client\Exception\HttpException
CURL async exception: Http\Client\Common\Exception\ClientErrorException
CURL sync exception: Http\Client\Common\Exception\ClientErrorException
React async status: 404
React promise state: fulfilled
React sync exception: Http\Client\Common\Exception\ClientErrorException
Socket async exception: Http\Client\Common\Exception\ClientErrorException
Socket sync exception: Http\Client\Common\Exception\ClientErrorException

I think this could be closed.

mekras commented 8 years ago

On 4XX and 5XX response call to $promise->wait() Curl and Guzzle throw an exception.

Latest curl-client did not throws an exception in wait(). Can you show example code?

tuupola commented 8 years ago

https://gist.github.com/tuupola/9862b11cc8fb6f7131545fffea900ee5

When using ErrorPlugin that is. Thanks for pointing out the mistake in the Gist. That was the main source of confusion. In the end it is only React which behaves differently.

Current dev-master of curl-client fixes the original problem for me. Thanks!

mekras commented 8 years ago

Fixed version released: https://github.com/php-http/curl-client/releases/tag/v1.6.1