nategood / httpful

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

Add new Exception #276

Closed andrzejd-pl closed 4 years ago

andrzejd-pl commented 6 years ago

If response can not parse to json, should be throw precise exception (JsonParseException) instead universal exception.

Cosmologist commented 5 years ago

You stole my PR :) I will not create a new one, but I ask you to change the method:

    public function parse($body)
    {
        $body = $this->stripBom($body);
        if (empty($body))
            return null;
        $parsed = json_decode($body, $this->decode_as_array);
        if (is_null($parsed) && 'null' !== strtolower($body)) {
            $exceptionMessage = 'Unable to parse response as JSON';

            if (function_exists('json_last_error_msg')) {
                $jsonMessage = json_last_error_msg();
                if ($jsonMessage !== false && $jsonMessage !== 'No error') {
                    $exceptionMessage .= ': ' . $jsonMessage;
                }
            }
            throw new \Exception($exceptionMessage);
        }
        return $parsed;
    }

in composer.json php>=5.3 - json_last_error_msg added at 5.5.0 json_last_error_msg can return FALSE or useless 'No error'

nategood commented 4 years ago

Finally dropped 5.* (18 months later) so json_last_error_msg is fine now.