-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Illuminate\Http\JsonResponse::setData()
falsely assumes that toJson()
calls json_encode
#42107
Comments
That is not the point of the issue. Please read carefully again. Of course, The problem is that |
@nagmat84 I see, thanks for clarifying that. I also see there's no way to clear To proceed with this can you provide me with a value that's invalid for |
I am not quite sure, why you need such a value, because the problem is caused by a json_encode(['a' => acos(2)]);
$result = json_last_error(); // $result equals 7 |
Going to re-open to get @taylorotwell's thoughts on this. Thanks |
I believe the description is precise to the point. What exactly is unclear about it? Anway:
|
@nagmat84 nothing is unclear here. Just want to get Taylor's thoughts. Using |
@nagmat84 what if we call |
Objectively speaking this would work. Personally, I don't like it. For me it feels like a (nasty) work-around. However, this is also due to the fact that I don't like the way how error are handled in Laravel at all. IMHO, using custom exceptions is the right way, see #40021 and #40020. But that is off-topic right now and nothing which can be solved without breaking backward compatibility. |
Thanks, I sent in a PR for this: #42125 |
Description
Details
The method
Illuminate\Http\JsonResponse::setData()
calls$data->toJson()
on the provided$data
, if$data
is an instance ofJsonable
. Otherwise,Illuminate\Http\JsonResponse::setData()
usesjson_encode
directly. Later,Illuminate\Http\JsonResponse::setData()
usesjson_last_error()
in order to check whether there has been any error.This is problematic, because this approach silently (and falsely) assumes that
toJson
usesjson_last_error()
internally. Please consider, that$data
might use a completely different implementation. For example, a trivial object could simply return a statically coded string.In this case,
json_last_error()
may not be cleared, but return a previous, stale error message from some completely unrelated former JSON operation (e.g. see #42106).Proposed Solution
As
setData
is going to throw an exception anyway, the easiest solution would be to use the optionJSON_THROW_ON_ERROR
and letjson_encode
throw the exception. Consequently,setData
throws an exception which is not caused by the current operation.This only makes the assumption that
toJson
will throw an exception, if anything goes wrong, which is a much slighter assumption than the previous one. This has also the nice benefit, thatsetData
throws a much more specificJsonException
than a genericInvalidArgumentException
.The text was updated successfully, but these errors were encountered: