Skip to content

Commit d683f7c

Browse files
Copilotswissspidy
andcommitted
Add HTTP error handling to wp core check-update command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c3116bc commit d683f7c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

features/core-check-update.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ Feature: Check for more recent versions
7070
"""
7171
---
7272
"""
73+
74+
Scenario: Check update shows warning when version check API fails
75+
Given a WP install
76+
And that HTTP requests to `api.wordpress.org` will respond with:
77+
"""
78+
HTTP/1.1 500 Internal Server Error
79+
"""
80+
81+
When I run `wp core check-update --force-check`
82+
Then STDERR should contain:
83+
"""
84+
Warning: Failed to check for updates
85+
"""

src/Core_Command.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
*/
3232
class Core_Command extends WP_CLI_Command {
3333

34+
/**
35+
* Stores HTTP API errors encountered during version check.
36+
*
37+
* @var \WP_Error|null
38+
*/
39+
private $version_check_error = null;
40+
3441
/**
3542
* Checks for WordPress updates via Version Check API.
3643
*
@@ -93,7 +100,17 @@ public function check_update( $args, $assoc_args ) {
93100
);
94101
$formatter->display_items( $updates );
95102
} else {
96-
WP_CLI::success( 'WordPress is at the latest version.' );
103+
// If there was an HTTP error during version check, show a warning
104+
if ( $this->version_check_error ) {
105+
WP_CLI::warning(
106+
sprintf(
107+
'Failed to check for updates: %s',
108+
$this->version_check_error->get_error_message()
109+
)
110+
);
111+
} else {
112+
WP_CLI::success( 'WordPress is at the latest version.' );
113+
}
97114
}
98115
}
99116

@@ -1439,8 +1456,18 @@ private function get_download_url( $version, $locale = 'en_US', $file_type = 'zi
14391456
*/
14401457
private function get_updates( $assoc_args ) {
14411458
$force_check = Utils\get_flag_value( $assoc_args, 'force-check' );
1459+
1460+
// Reset error tracking
1461+
$this->version_check_error = null;
1462+
1463+
// Hook into HTTP API debug to capture errors during version check
1464+
add_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10, 5 );
1465+
14421466
wp_version_check( [], $force_check );
14431467

1468+
// Remove the hook after version check
1469+
remove_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10 );
1470+
14441471
/**
14451472
* @var object{updates: array<object{version: string, locale: string, packages: object{partial?: string, full: string}}>}|false $from_api
14461473
*/
@@ -1496,6 +1523,27 @@ private function get_updates( $assoc_args ) {
14961523
return array_values( $updates );
14971524
}
14981525

1526+
/**
1527+
* Handles the http_api_debug action to capture HTTP errors during version check.
1528+
*
1529+
* @param array|WP_Error $response HTTP response or WP_Error object.
1530+
* @param string $context Context of the HTTP request.
1531+
* @param string $class HTTP transport class name.
1532+
* @param array $args HTTP request arguments.
1533+
* @param string $url URL being requested.
1534+
*/
1535+
private function capture_version_check_error( $response, $context, $class, $args, $url ) {
1536+
// Only capture errors for the version check API
1537+
if ( false === strpos( $url, 'api.wordpress.org/core/version-check' ) ) {
1538+
return;
1539+
}
1540+
1541+
// Store the error if the response is a WP_Error
1542+
if ( is_wp_error( $response ) ) {
1543+
$this->version_check_error = $response;
1544+
}
1545+
}
1546+
14991547
/**
15001548
* Clean up extra files.
15011549
*

0 commit comments

Comments
 (0)