Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs in ElasticSearch request error reporting #371

Open
tcrsavage opened this issue Jan 26, 2022 · 2 comments
Open

Bugs in ElasticSearch request error reporting #371

tcrsavage opened this issue Jan 26, 2022 · 2 comments
Labels
bug Existing functionality isn't behaving as expected

Comments

@tcrsavage
Copy link

Steps to reproduce:

  1. Delete a post, triggering immediate deletion request to ES (this does not go through the ES bulk request queue) - triggers false positive error PHP Warning: Error in ElasticSearch request: (0) in /usr/src/app/vendor/altis/cloud/inc/namespace.php on line 379 with no debug data. In this case should probably trigger a warning that the request is non blocking, thus response cannot be inspected for errors

  2. Send a large request which triggers split_large_ep_request, this will bypass request logging due to direct use of wp_remote_request. Logging requests on a lower level should solve this: see example below

add_action( 'http_api_debug', __NAMESPACE__ . '\\enhanced_es_debug', 10, 5 );

/**
 * Do an error log of failed ES requests
 * Lower level than inbuilt altis one, as it is able to catch any request made to ES via wp_remote_{x}
 *
 * @param array|WP_Error $response
 * @param string $context
 * @param string $class
 * @param array $parsed_args
 * @param string $url
 * @return void
 */
function enhanced_es_debug( $response, $context, $class, $parsed_args, $url ) {
	if ( $context !== 'response' ) {
		return;
	}

	// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
	$host = parse_url( $url, PHP_URL_HOST );

	if ( ELASTICSEARCH_HOST !== $host ) {
		return;
	}

	$request_response_body = wp_remote_retrieve_body( $response );
	$request_response_code = (int) wp_remote_retrieve_response_code( $response );
	$is_valid_res = ( $request_response_code >= 200 && $request_response_code <= 299 );
	$type = $parsed_args['method'] ?? 'GET?';

	// Backup check for errors, sometimes the response is ok but the query
	// response JSON contains errors.
	$has_errors = strpos( $request_response_body, '"errors":true' ) !== false;

	if ( is_wp_error( $response ) ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		trigger_error( sprintf( 'RBMH Error in ElasticPress request: WP_Error %s %s (%s) %s', $type, $response->get_error_message(), $response->get_error_code(), $url ), E_USER_WARNING );
	} elseif ( ! $is_valid_res || $has_errors ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		trigger_error( sprintf( 'RBMH Error in ElasticPress request: %s %s (%s) %s', $type, $request_response_body, $request_response_code, $url ), E_USER_WARNING );
	}
}
@tcrsavage tcrsavage added the bug Existing functionality isn't behaving as expected label Jan 26, 2022
@itc-michael-tran
Copy link

itc-michael-tran commented Feb 7, 2024

Hi @tcrsavage , did you solve this bug?
I get the same error while trying to save my ACF fields

Screen Shot 2024-02-07 at 09 25 16

@tcrsavage
Copy link
Author

tcrsavage commented Feb 9, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing functionality isn't behaving as expected
Projects
None yet
Development

No branches or pull requests

2 participants