Automattic/regenerate-thumbnails

jqXHR.responseJSON can be undefined

Closed this issue · 3 comments

Hello,

Thank you for the awesome plugin.

I have encountered a minor issue. On some servers errors like HTTP 500 can sometimes be returned in HTML or text format instead of JSON, even if it was AJAX request. When such an error happens on the multiple thumbnails regeneration page, the whole process stops and the console contains an error about jqXHR.responseJSON being undefined (sorry didn't take the screenshot). I believe the expected behaviour is that image which received an error response is skipped and the regeneration process is continued for other images.

If the regeneration request for a single attachment fails, it should continue onto the next one and log the error.

I believe the only place it'll truly bail out is if it fails to fetch a chunk of attachments 3 times in a row. I'm not sure if it really should continue in that case because then 25 images are being skipped. Better to fix the issue with your server.

@Viper007Bond I'm sorry, I think I wasn't clear enough.

If the regeneration request for a single attachment fails, it should continue onto the next one and log the error.

Yes, it should. But in the case that I am talking about it doesn't, because a JS syntax error happens and the regen process stops completely.

error

I am talking about this JS error. I think it happens because error 500 was returned in HTML format but the code expected it to be in JSON. I think it's pretty common on shared hosting providers that 500 status errors are returned in HTML even if it was a JSON API request.

In my case I was trying to regenerate a bunch of images on a client's website, but because of low PHP memory limits some regeneration requests were randomly failing with error 500. I think it happened to about 5 images out of 100, and it would be ok if they were just skipped because in the end I could just go regenerate them directly from media library.

I have created a demo site to demonstrate the issue.
Username: demo
Password: 4Muuo6Zt49BX

Try regenerating the images and the process will stop on the second image.

To simulate the 500 error for the second image I've added the following code to theme's functions.php:

add_action( 'parse_request', function() {
	if ( '/regenerate-thumbnails/v1/regenerate/616' === $GLOBALS['wp']->query_vars['rest_route'] ) {
		status_header( 500 );
		echo "<head><title>Internal Server Error</title></head><body><h1>500 Internal Server Error</h1></body>";
		die();
	}
}, 0);

Ah, gotcha! Thanks, I'll investigate.