From 695e8e053f1fdd008449d48e86e5a1885333346f Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Tue, 14 Apr 2026 16:33:38 +0200 Subject: [PATCH] fix(ajax): handle failed requests gracefully Add .fail() handler to the AJAX call so a network error or server failure shows an error row instead of stopping the entire chain. Wrap JSON.parse in try/catch for malformed responses. Processing continues to the next plugin in both cases. --- js/plugin-report.js | 23 ++++++++++++++++++----- rt-plugin-report.php | 2 ++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/js/plugin-report.js b/js/plugin-report.js index d4f517f..36a106b 100644 --- a/js/plugin-report.js +++ b/js/plugin-report.js @@ -54,15 +54,28 @@ jQuery(document).ready( function( $ ){ }; jQuery.post( ajaxurl, data, function(response) { - // parse the response - const obj = JSON.parse(response); - // replace the temporary table row with the new data - $('#plugin-report-table .plugin-report-row-temp-' + slug ).replaceWith( obj.html ); - // on to the next... + try { + const obj = JSON.parse(response); + $('#plugin-report-table .plugin-report-row-temp-' + slug ).replaceWith( obj.html ); + } catch (e) { + rtpr_replace_with_error( slug ); + } + rtpr_process_next_plugin(); + }).fail( function() { + rtpr_replace_with_error( slug ); rtpr_process_next_plugin(); }); } + + function rtpr_replace_with_error( slug ){ + const cols = plugin_report_vars.cols_per_row || 9; + const msg = plugin_report_vars.ajax_error || 'Error'; + $('#plugin-report-table .plugin-report-row-temp-' + slug ).replaceWith( + '' + msg + '' + ); + } + // kick things off rtpr_process_next_plugin(); diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 6b76f78..5b524a3 100644 --- a/rt-plugin-report.php +++ b/rt-plugin-report.php @@ -190,6 +190,8 @@ public function enqueue_assets( $hook ) { 'export_btn' => __( 'Export .csv file', 'plugin-report' ), 'plugin_url_header' => __( 'Plugin URL', 'plugin-report' ), 'author_url_header' => __( 'Author URL', 'plugin-report' ), + 'ajax_error' => __( 'Error fetching plugin info.', 'plugin-report' ), + 'cols_per_row' => self::COLS_PER_ROW, ); wp_localize_script( 'plugin-report-js', 'plugin_report_vars', $vars ); // Enqueue admin CSS file.