Search code examples
phperror-handlingwordpress-rest-api

error_reporting(E_ALL) fails with no log or error message


I am using the WordPress REST API and in my main plugin file I set:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

I'm getting some weird behavior. For instance in a function/endpoint I have something like:

function test($request){
    $request['id'] <- this works fine here

    but if I add another function like this I get a silent error

    $validID = check_id($request['id']);
}

So when I do this my return from the endpoint is blank and I get this error:

has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 

Now if I set error_reporting to:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

My endpoint works just fine. Not sure what is happening here. I don't get any php error logs and this makes it extremely difficult to troubleshoot. So should I just NOT use E_ALL?

I also tested the endpoint by leaving out a semi colon. I get the same result except now I get an empty response for both error_reporting(E_ALL) and error_reporting(E_ERROR | E_WARNING | E_PARSE). I'd love to get some kind of log/return to tell me whats wrong.


Solution

  • Unfortunately a lot of WordPress code out there is written without WP_DEBUG enabled, which means without E_ALL level. As result it tends to be full of notices and deprecation issues developers don't see and don't bother to correct.

    So raising error reporting can often trigger issues entirely unrelated to your own code.

    Since your immediate problem is lack of obvious error message/log, you should experiment with WP_DEBUG_DISPLAY and WP_DEBUG_LOG configuration to see if that helps.

    Generally it's preferable to use constants to control WP's error handling centrally. When extensions start to mess with this it's... well, a mess. :)

    Another thing you could try is a dedicated error handler. I made and use a wps plugin that wraps whoops handler for WP context. For API errors it tries to override the output and return a meaningful trace for what happened.