-
Notifications
You must be signed in to change notification settings - Fork 239
Improve invalid REST API response handling #1074
Description
In #984, we've worked around the situation where the server (REST API) sends a malformed response, e.g., by including a PHP notice from some plugin. The __VP__ key has been added to every JSON response so that the client-side JavaScript code can understand what is the real response. However, after some internal discussion, we're not sure this is the right approach.
This issue is about reconsidering our options here. Some initial comments:
-
The current approach (as per Make AJAX work with PHP error / notice output #984) modifies the object and feels slightly weird. We'll probably abandon it.
-
Every response could be wrapped in an object like this:
{ __VP__: true, data: theActualValue }The client would use something like
response.body.data.theActualValueinstead ofresponse.body.theActualValue. This would also provide the opportunity to send additional metadata with every response should we need that, like what VersionPress or WP version are running on the server. -
The client could also reject to work if the response is malformed. I think we considered this Make AJAX work with PHP error / notice output #984 but in the end decided it is better for the users if the UI works despite some warnings in it. However, it could be argued that sending notices and such to the client is a security risk / server misconfiguration and we could just show some good error to guide the users to fix their server setup. This is a valid approach.
Let's do some research because the other projects relying on WP REST API must hit the same issue as well.