← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-ai.php
+10
-5
16.2
→
16.3-a.7
View file →
| @@ -236,12 +236,17 @@ | ||
| 236 | 236 | if ( is_wp_error( $response ) ) { |
| 237 | 237 | return $response; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - $data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 240 | + $status_code = wp_remote_retrieve_response_code( $response ); | |
| 241 | + $data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 241 | 242 | |
| 242 | - if ( empty( $data->cache_key ) ) { | |
| 243 | - return new WP_Error( 'invalid_ask_response', __( 'Invalid response from the server.', 'jetpack' ), 400 ); | |
| 243 | + // Forward the real upstream error instead of a generic 500. See SEARCH-351. | |
| 244 | + if ( $status_code >= 400 || empty( $data->cache_key ) ) { | |
| 245 | + $code = isset( $data->code ) && is_scalar( $data->code ) ? (string) $data->code : 'invalid_ask_response'; | |
| 246 | + $message = isset( $data->message ) && is_scalar( $data->message ) ? (string) $data->message : __( 'Invalid response from the server.', 'jetpack' ); | |
| 247 | + $status = $status_code >= 400 ? $status_code : 400; | |
| 248 | + return new WP_Error( $code, $message, array( 'status' => $status ) ); | |
| 244 | 249 | } |
| 245 | 250 | |
| 246 | 251 | return $data; |
| 247 | 252 | } |
| @@ -257,9 +262,9 @@ | ||
| 257 | 262 | $comment = $request->get_param( 'comment' ); |
| 258 | 263 | $cache_key = $request->get_param( 'cache_key' ); |
| 259 | 264 | |
| 260 | 265 | if ( strpos( $cache_key, 'jp-search-ai-' ) !== 0 ) { |
| 261 | - return new WP_Error( 'invalid_cache_key', __( 'Invalid cached context for the answer feedback.', 'jetpack' ), 400 ); | |
| 266 | + return new WP_Error( 'invalid_cache_key', __( 'Invalid cached context for the answer feedback.', 'jetpack' ), array( 'status' => 400 ) ); | |
| 262 | 267 | } |
| 263 | 268 | |
| 264 | 269 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 265 | 270 | $response = Client::wpcom_json_api_request_as_blog( |
| @@ -284,9 +289,9 @@ | ||
| 284 | 289 | |
| 285 | 290 | $data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 286 | 291 | |
| 287 | 292 | if ( 'ok' !== $data ) { |
| 288 | - return new WP_Error( 'invalid_feedback_response', __( 'Invalid response from the server.', 'jetpack' ), 400 ); | |
| 293 | + return new WP_Error( 'invalid_feedback_response', __( 'Invalid response from the server.', 'jetpack' ), array( 'status' => 400 ) ); | |
| 289 | 294 | } |
| 290 | 295 | |
| 291 | 296 | return $data; |
| 292 | 297 | } |