Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] JsonResponse throws an incomprehensible exception if json_encode fails #9903

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

Closed
chrisguitarguy opened this issue Dec 30, 2013 · 0 comments

Comments

@chrisguitarguy
Copy link
Contributor

In JsonResponse::setData json_encode is called then and then JsonEncode::update is called to deal with setting up the content.

If no JS callback is specified and json_encode fails -- returns false -- Response::setContent throws an UnexpectedValueException saying that only strings or objects that have a __toString method are allowed.

I ran into this with an invalid UTF8 character that came out of MySQL.

Seems like this could be improved by checking for a false result on the json_encode call and using json_last_error to generate a nicer error message.

Would that be too detailed for JsonResponse? Should it be up to the caller to make sure they give JsonResponse::setData something valid? Even if it's very hard to tell that something may not work?

fabpot added a commit that referenced this issue Dec 31, 2013
…s passed to JsonResponse class (stloyd)

This PR was merged into the 2.3 branch.

Discussion
----------

[HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| BC breaks?    | no*
| Tests pass?   | yes
| Fixed tickets | #9903
| License       | MIT

\* as described in mentioned issue, before this PR there was thrown exception `UnexpectedValueException`, which was not correct, but I guess some people trying to hide the bug could use `try {} catch` on it.

Commits
-------

38287e7 [HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class
@fabpot fabpot closed this as completed Dec 31, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants