-
Notifications
You must be signed in to change notification settings - Fork 27
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
Added check for json_decode result #40
Conversation
Thank you for your contribution! I agree with your judgement that it's better to return Also, could you please:
|
Hi, I've added a new commit that complies with PSR-2 coding style guide. About the distinguishing between the API returning invalid JSON vs |
Thank you, it's looking good now!
The test you've added looks good!
If anything, it should not silently ignore the error. But to be honest, I've never seen this happen. |
I have a suggestion but this may need a lot of work to do but what if we created an error handler function where it will be called when something went wrong and developers can handle the error accordingly, just like Laravel's handler. I imagined it has a function like this:
And on our source we can do something like this:
Then the developers that uses this can do:
This is a bit flexible since the dev's can handle the errors easily and we can list the possible errors on the GitHub README. What do you think about this? ALTERNATIVELY we could just return |
Hi, should we just return false if the server responded with broken JSON? |
I really don't like the error handler solution for a few reasons. For starters, with any other case where the API does not respond with the expected data type (e.g. most of the XML endpoints), you just get an I don't think it's necessary to have checks to be able to gracefully handle every conceivable kind of issue, because it just makes the code unnecessarily complicated, hard to read and slow to execute. On really spectacular failures, you'd typically see either the PHP code crash and go through the regular crash recovery system or a Guzzle exception bubbling up. Which is the "right" way to handle errors: the PHP error handler, or throwing exceptions. Having the error handler exactly as you proposed them means that:
Passing a callable to the client to handle error would be a slightly nicer way to handle it. But even then, you're just reproducing the behavior of PHP's To summarize, if you need to deal with some catastrophic failure, exceptions are the way to go. But with the current implementation, in case there is invalid JSON, |
Alright then, shall we merge? |
Fix #39
Checked
json_decode
result if it's null, then return anull
.On line 63, I've added
throw new \RuntimeException('Failed to decode JSON data: ' . $this->getData());
which can be uncommented if you prefer Exception to be thrown instead of returningnull
.