From e564fe0d3a5d24651d6dc6c059678712955b5ebc Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 23:36:25 +0200 Subject: [PATCH] Boost: Fix ISA showing an error if no report was found (#40660) * Fix boost API client not properly parsing errors returned by wpcom * Fix showing error when a report wasn't found User might not have requested an analysis yet. That doesn't mean the UI should show an error. This restores behavior from pre-Boost 3.6.0. * Add changelogs * Revert boost core changes * remove changelog * Revert "Revert boost core changes" This reverts commit afa7179a61bdc347cb203233f9f4f33778ce2a29. * Revert "remove changelog" This reverts commit 870e13d0c0fd09966e3f36d1834bb9b5d67c6cff. * Clarify comment --------- Co-authored-by: Adnan Haque --- ...fix-not-parsing-some-wpcom-errors-properly | 4 ++++ .../boost-core/src/lib/class-utils.php | 23 +++++++++++++++---- .../recommendations-meta.tsx | 2 +- ...fix-isa-showing-error-if-no-report-present | 4 ++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 projects/packages/boost-core/changelog/fix-not-parsing-some-wpcom-errors-properly create mode 100644 projects/plugins/boost/changelog/fix-isa-showing-error-if-no-report-present diff --git a/projects/packages/boost-core/changelog/fix-not-parsing-some-wpcom-errors-properly b/projects/packages/boost-core/changelog/fix-not-parsing-some-wpcom-errors-properly new file mode 100644 index 0000000000000..d24d162efc1e6 --- /dev/null +++ b/projects/packages/boost-core/changelog/fix-not-parsing-some-wpcom-errors-properly @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +General: Fixed not parsing error responses from WordPress.com properly. diff --git a/projects/packages/boost-core/src/lib/class-utils.php b/projects/packages/boost-core/src/lib/class-utils.php index 848cf3009c0b3..52207e115f075 100644 --- a/projects/packages/boost-core/src/lib/class-utils.php +++ b/projects/packages/boost-core/src/lib/class-utils.php @@ -133,11 +133,26 @@ public static function send_wpcom_request( $method, $endpoint, $args = null, $bo $code ); + /* + * Normalize error responses from WordPress.com. + * + * When WordPress.com returns an error from Boost Cloud, the body contains + * statusCode and error. When it returns a WP_Error, it contains code and message. + */ // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - $err_code = empty( $data['statusCode'] ) ? 'http_error' : $data['statusCode']; - $message = empty( $data['error'] ) ? $default_message : $data['error']; - - return new \WP_Error( $err_code, $message ); + if ( isset( $data['statusCode'] ) && isset( $data['error'] ) ) { + // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $data_code = $data['statusCode']; + $data_message = $data['error']; + } elseif ( isset( $data['code'] ) && isset( $data['message'] ) ) { + $data_code = $data['code']; + $data_message = $data['message']; + } + + $error_code = empty( $data_code ) ? 'http_error' : $data_code; + $message = empty( $data_message ) ? $default_message : $data_message; + + return new \WP_Error( $error_code, $message ); } return $data; diff --git a/projects/plugins/boost/app/assets/src/js/features/image-size-analysis/recommendations-meta/recommendations-meta.tsx b/projects/plugins/boost/app/assets/src/js/features/image-size-analysis/recommendations-meta/recommendations-meta.tsx index 049d1bb448a59..f475249d3f8b6 100644 --- a/projects/plugins/boost/app/assets/src/js/features/image-size-analysis/recommendations-meta/recommendations-meta.tsx +++ b/projects/plugins/boost/app/assets/src/js/features/image-size-analysis/recommendations-meta/recommendations-meta.tsx @@ -87,7 +87,7 @@ const RecommendationsMeta: React.FC< Props > = ( { isCdnActive } ) => { isaRequest.isError; const getErrorMessage = ( report: typeof isaReport ) => { - if ( report?.status === 'error' || report?.status === 'not-found' ) { + if ( report?.status === 'error' ) { return report.message; } diff --git a/projects/plugins/boost/changelog/fix-isa-showing-error-if-no-report-present b/projects/plugins/boost/changelog/fix-isa-showing-error-if-no-report-present new file mode 100644 index 0000000000000..ec4939628d4b8 --- /dev/null +++ b/projects/plugins/boost/changelog/fix-isa-showing-error-if-no-report-present @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +UI: Fixed showing an error if no ISA report was found.