7

We've got some custom endpoints set up that do various things, which we access via /wp/wp-admin/admin-ajax.php?action=some_action

However whenever there is an error as we're developing, such as syntax, logical, fatal etc, we simply get "500 Internal Server Error"

Every other page on the site when there's an error, it gives us the PHP error.

We have to then open our PHP Log file to see the error.

Is there something in wordpress that disables displaying of errors on these URLs? and if so, how can we prevent this to allow rendering of the errors on the browser?

5
  • If you look at the response in the network tab of the browser developer tools you’ll see the error. Mar 26, 2018 at 10:46
  • That's the whole problem - we just get a "500 internal server error" response - regardless of how we get there, even if you load the url directly in your browser
    – owenmelbz
    Mar 26, 2018 at 12:09
  • Please read this wordpress.stackexchange.com/questions/96554/…
    – bueltge
    Mar 26, 2018 at 13:30
  • @bueltge Sorry I've read it, but do not understand what is relevant? I cannot see anywhere that they are discussing why error messages do not render?
    – owenmelbz
    Mar 26, 2018 at 14:21
  • Errors from ajax requests are never rendered in your browser directly. If error reporting is turned on you can see error messages in the network console. Bueltges link helps to debug that way.
    – Luckyfella
    Dec 14, 2018 at 23:50

3 Answers 3

5

WordPress by default hide errors for ajax request call. This can be confirmed from the source file wp-includes/load.php#L352, here:

if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
    @ini_set( 'display_errors', 0 );
}

See the function wp_doing_ajax() is being used in the conditional statement thus the display_errors is being set.

To workaround this, you need to turn on error reporting manually at top of your ajax function call as suggested by @Friss.

4
  • THANK YOU!!! I know obviously you never want to hack the core, but in a local environment only for troubleshooting purposes only, when the ajax is being called by a plugin you don't want to mess with or have to time to figure out - temporarily commenting out the line noted above in /wp-includes/load.php will allow the output of more specific error messages and let you track down where the problem is coming from. Sep 25, 2019 at 19:58
  • 1
    There is a filter at the top of load.php that bypasses it: add_filter( 'enable_wp_debug_mode_checks', '__return_false' );
    – CragMonkey
    Apr 25, 2020 at 18:48
  • @Cragmonkey Please explain how one could use that hook on their ajax call. May 10, 2020 at 1:58
  • @Rahil Wazir ... You would temporarily create that hook elsewhere in your plugin while debugging.
    – CragMonkey
    May 11, 2020 at 22:36
3

You can try to add these two lines at the very top of tour script file

error_reporting(E_ALL); 
ini_set("display_errors", 1);

It tells php to report all kind of errors and overrides its default settings to display them.

2
  • Hi, This does turn on error reporting - however, we're specifically trying to find out WHY it gets turned off for the admin-ajax.php as if we set it in the bottom of wp-config for example, it gets overwritten somewhere for the ajax calls
    – owenmelbz
    Mar 27, 2018 at 8:47
  • worked thanks buddy Apr 2 at 17:29
-1

You can try the WP_Ajax_Response

$response = array(
   'what'=>'stuff',
   'action'=>'delete_something',
   'id'=>new WP_Error('oops','I had an accident.'),
   'data'=>'Whoops, there was a problem!'
);
$xmlResponse = new WP_Ajax_Response($response);
$xmlResponse->send();

Read more https://codex.wordpress.org/Function_Reference/WP_Ajax_Response

2
  • Sorry but this is not what we're asking for - our issue is that php errors are not displaying, when we need them to
    – owenmelbz
    Mar 26, 2018 at 12:10
  • what if there are sql or any other errors? Feb 3, 2021 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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