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?
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?
Share Improve this question asked Mar 26, 2018 at 10:19 owenmelbzowenmelbz 1711 gold badge1 silver badge4 bronze badges 5- If you look at the response in the network tab of the browser developer tools you’ll see the error. – Jacob Peattie Commented 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 Commented Mar 26, 2018 at 12:09
- Please read this wordpress.stackexchange/questions/96554/… – bueltge Commented 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 Commented 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 Commented Dec 14, 2018 at 23:50
3 Answers
Reset to default 6WordPress 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.
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.
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/Function_Reference/WP_Ajax_Response