| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress; |
| 10 | 10 | |
| 11 | -use ElasticPress\Utils as Utils; | |
| 11 | +use ElasticPress\Utils; | |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 14 | exit; // Exit if accessed directly. |
| 15 | 15 | } |
| @@ -73,8 +73,16 @@ | ||
| 73 | 73 | */ |
| 74 | 74 | protected $nodes = 0; |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | + * Failed queries and their errors. | |
| 78 | + * | |
| 79 | + * @since 5.0.1 | |
| 80 | + * @var string | |
| 81 | + */ | |
| 82 | + protected $failed_queries = []; | |
| 83 | + | |
| 84 | + /** | |
| 77 | 85 | * Makes an api call to elasticsearch endpoint |
| 78 | 86 | * |
| 79 | 87 | * @param string $path Endpoint path to query |
| 80 | 88 | * @since 3.2 |
| @@ -82,15 +90,31 @@ | ||
| 82 | 90 | */ |
| 83 | 91 | protected function remote_request_helper( $path ) { |
| 84 | 92 | $request = Elasticsearch::factory()->remote_request( $path ); |
| 85 | 93 | |
| 86 | - if ( is_wp_error( $request ) || empty( $request ) ) { | |
| 94 | + if ( empty( $request ) ) { | |
| 87 | 95 | return false; |
| 88 | 96 | } |
| 89 | 97 | |
| 90 | - $body = wp_remote_retrieve_body( $request ); | |
| 98 | + if ( is_wp_error( $request ) ) { | |
| 99 | + $this->failed_queries[] = [ | |
| 100 | + 'path' => $path, | |
| 101 | + 'error' => $request->get_error_message(), | |
| 102 | + ]; | |
| 103 | + return false; | |
| 104 | + } | |
| 91 | 105 | |
| 92 | - return json_decode( $body, true ); | |
| 106 | + $body = wp_remote_retrieve_body( $request ); | |
| 107 | + $return = json_decode( $body, true ); | |
| 108 | + | |
| 109 | + if ( ! empty( $return['errors'] ) ) { | |
| 110 | + $this->failed_queries[] = [ | |
| 111 | + 'path' => $path, | |
| 112 | + 'error' => wp_json_encode( $return['errors'] ), | |
| 113 | + ]; | |
| 114 | + } | |
| 115 | + | |
| 116 | + return $return; | |
| 93 | 117 | } |
| 94 | 118 | |
| 95 | 119 | /** |
| 96 | 120 | * Makes api calls and organizes data depending on the specified context. |
| @@ -289,8 +313,27 @@ | ||
| 289 | 313 | $suffix = array( '', 'KB', 'MB', 'GB', 'TB' ); |
| 290 | 314 | $f_base = floor( $base ); |
| 291 | 315 | |
| 292 | 316 | return round( pow( 1024, $base - floor( $base ) ), 1 ) . $suffix[ $f_base ]; |
| 317 | + } | |
| 318 | + | |
| 319 | + /** | |
| 320 | + * Return all failed queries. | |
| 321 | + * | |
| 322 | + * @return array | |
| 323 | + * @since 5.0.1 | |
| 324 | + */ | |
| 325 | + public function get_failed_queries() { | |
| 326 | + return $this->failed_queries; | |
| 327 | + } | |
| 328 | + | |
| 329 | + /** | |
| 330 | + * Clear all failed queries registered. | |
| 331 | + * | |
| 332 | + * @since 5.0.1 | |
| 333 | + */ | |
| 334 | + public function clear_failed_queries() { | |
| 335 | + $this->failed_queries = []; | |
| 293 | 336 | } |
| 294 | 337 | |
| 295 | 338 | /** |
| 296 | 339 | * Return singleton instance of class |