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

Introduce new 'halt_on_error' to overload exit #4383

Merged
merged 3 commits into from Oct 2, 2017
Jump to file or symbol
Failed to load files and symbols.
+16 −3
Diff settings

Always

Just for now

View
@@ -581,6 +581,7 @@ function replace_path_consts( $source, $path ) {
function http_request( $method, $url, $data = null, $headers = array(), $options = array() ) {
$cert_path = '/rmccue/requests/library/Requests/Transport/cacert.pem';
$halt_on_error = ! isset( $options['halt_on_error'] ) || (bool) $options['halt_on_error'];
if ( inside_phar() ) {
// cURL can't read Phar archives
$options['verify'] = extract_from_phar(
@@ -594,7 +595,11 @@ function http_request( $method, $url, $data = null, $headers = array(), $options
}
}
if ( empty( $options['verify'] ) ) {
WP_CLI::error( 'Cannot find SSL certificate.' );
$error_msg = 'Cannot find SSL certificate.';
if ( $halt_on_error ) {
WP_CLI::error( $error_msg );
}
throw new \RuntimeException( $error_msg );
}
}
@@ -604,15 +609,23 @@ function http_request( $method, $url, $data = null, $headers = array(), $options
} catch ( \Requests_Exception $ex ) {
// CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7.
if ( 'curlerror' !== $ex->getType() || ! in_array( curl_errno( $ex->getData() ), array( CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) {
\WP_CLI::error( sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() ) );
$error_msg = sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() );
if ( $halt_on_error ) {
WP_CLI::error( $error_msg );
}
throw new \RuntimeException( $error_msg, null, $ex );
}
// Handle SSL certificate issues gracefully
\WP_CLI::warning( sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ) );
$options['verify'] = false;
try {
return \Requests::request( $url, $headers, $data, $method, $options );
} catch ( \Requests_Exception $ex ) {
\WP_CLI::error( sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() ) );
$error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() );
if ( $halt_on_error ) {
WP_CLI::error( $error_msg );
}
throw new \RuntimeException( $error_msg, null, $ex );
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.