You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In JsonResponse::setDatajson_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?
The text was updated successfully, but these errors were encountered:
…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
In
JsonResponse::setData
json_encode
is called then and thenJsonEncode::update
is called to deal with setting up the content.If no JS callback is specified and
json_encode
fails -- returnsfalse
--Response::setContent
throws anUnexpectedValueException
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 thejson_encode
call and usingjson_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 giveJsonResponse::setData
something valid? Even if it's very hard to tell that something may not work?The text was updated successfully, but these errors were encountered: