Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Exception #276

Merged
merged 3 commits into from
Jan 25, 2020
Merged

Add new Exception #276

merged 3 commits into from
Jan 25, 2020

Conversation

andrzejd-pl
Copy link
Contributor

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

@Cosmologist
Copy link

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'

@andrzejd-pl andrzejd-pl changed the title Dev Add new Exception Sep 14, 2018
@nategood
Copy link
Owner

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

@nategood nategood merged commit cd9b569 into nategood:dev Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants