1

Happy day scenario: I implement WP REST API that sends JSONs to a JavaScript client, which parses it and displays the data to the user. All is fine.

What actually happens, inevitably: some WordPress code or plugin generates a PHP notice or warning, and the server is configured so that this garbage finds its way to the JSON response. (WP REST API tries to avoid that but for example if ini_set() is disallowed by server configuration, it can still happen.)

One way to approach is to simply blame it on the users – if their servers are configured so that they send PHP notices and warnings in production it's certainly bad. However, even the core REST infrastructure tries to support users who have badly configured servers and it's my goal as well in this case.

Is there a technique established for dealing with this? I can see two ways:

  1. Detect in JavaScript that the response is malformed, show some error and refuse to work.
  2. Modify the actual JSON by adding some unique key to it on the server and let the JavaScript client work with that. For example, instead of the plain value 123, it would be wrapped into an object like { "__VALID__": true, "data": 123 } and the JS code would use response.body.data instead of response.body.

Or is there some other approach?

  • What kind of response malformation are we talking about? Is it messing up the structure of the data? Shouldn't it inject any excess data as a separate object to response root? Data parsing logic should access desired data object directly , looping and sorting on response root level should be avoided because some kind of excess data might get to response (you can never be sure with WP and plugins) and you would include it to parsing logic which might break everything. Another better safe than sorry thing to do would be to wrap all your data to a custom named object (e.g domain name etc). – N00b Jun 1 '16 at 8:01
  • @N00b Instead of 123 (valid response), it gets something like Notice: Undefined variable...123. The trouble is that you cannot reliably capture notices on the server and wrap them inside your JSON, I think. – Borek Bernard Jun 1 '16 at 8:04
1

If you are creating classes, the first thing in the construct would be this. Or you can drop it in with an action as early as possible. Maybe plugins_loaded.

/*
     * If WP DEBUG is not on do NOT return any php warning, notices, and/or fatal errors.
     * Well If it is a fatal error then this return is FUBAR anyway...
     * We do this because some badly configured servers will return notices and warnings switch get prepended or appended to the rest response.
     */
    if( defined('WP_DEBUG') ){
        if( false === WP_DEBUG ){
            error_reporting(0);
        }
    }
  • so no way for the owner to know there are problems on his site? :( In any case this will not solve problems related to premature output in general. – Mark Kaplun Oct 27 '17 at 4:48
  • You shouldn't be getting any output from PHP warnings and notices if debug is off. If debug is on then the above error_reporting() function would never run and the user would see whats wrong. – Ernest Nov 6 '17 at 16:02
  • you can call it before plugins are loaded, WP core doesn't usually have notices and warnings. It's usually a plugin or a theme. – Ernest Nov 6 '17 at 16:04
  • "should not" do not mean "will not". How do you fix a problem if you do not know it is even there? And obviously in a plugin you can not call it before plugins are loaded. – Mark Kaplun Nov 6 '17 at 16:11
  • updated the anwser... muplugins hahaha woops :) I have seen some crazy things with rest and ajax calls where debug is off and errors still show with rest calls. Usually, yes not always, if you add error_reporting(0) as the first line in the rest api callback function then there will be no error returned. adding the if defined lets you debug the code if debug is on. – Ernest Nov 8 '17 at 0:10
0

You can not fix in your own plugin general problems caused by other parts of the software as there are just too many possibilities. It is the site owner's responsibility to run a clean operation if he expects to be able to use additional plugin/themes.

The only reasonable approach that you may have is to log this kind of responses in the browser console so it will be easier for you to track down why things are not working when a plugin user complains.

-1

You need to use a combination of exception catching and a custom error handler.

If you use this to suppress errors to the end user, you might want to add in some method that flags the error to the administrator/developer/yourself, so you are at least aware of the issue.

Alternatively, instead of suppressing the errors, you could return an object with both the return value, and an array of any errors, to the client. It would then be up to the client whether it did anything with that information. Whether you want to do this depends on who the clients are, and what sensitive information might be exposed by these errors. Of course you have complete control over what gets output, you don't need to just output the notices word for word.

  • 1
    The main question is, can I 100% reliably capture all error output (notices, warnings) just from PHP code, even if the server is badly configured? As a plugin vendor, I cannot control the server environment in which the WordPress site runs. – Borek Bernard Jun 1 '16 at 8:17
  • So long as your error handler is defined early, it should catch everything that comes after. Errors in the same file as the error handler could be a problem, and obviously any errors encountered before the error handler is parsed, other than that only fatal errors will cause it problems. There are work arounds for fatal errors. – AntonChanning Jun 1 '16 at 8:26
  • What if some other plugin is loaded by WordPress earlier than mine, generates PHP notice and sends it to the browser immediately? I cannot catch that by my code that hasn't been even loaded yet, right? – Borek Bernard Jun 1 '16 at 8:29
  • Well, just ensure yours loads first. See this comment: wordpress.org/support/topic/… – AntonChanning Jun 1 '16 at 8:35
  • You may also find this answer useful: wordpress.stackexchange.com/a/63146/5077 – AntonChanning Jun 1 '16 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.