Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3428,7 +3428,8 @@ function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->get_all_error_data(),
static function ( $status, $error_data ) {
return $error_data['status'] ?? $status;
// Note: $error_data may not be an array (e.g. stdClass), so is_array() check is intentional.
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add a single line comment so in future other can not reintroduce similar changes for Code Modernization

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an inline comment. The new test case test_error_to_response_with_stdclass_data would also catch any future regression.

},
500
);
Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,19 @@ public function test_error_to_response_with_additional_data() {
$this->assertSame( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] );
}

/**
* @ticket 64901
*/
public function test_error_to_response_with_stdclass_data() {
$error = new WP_Error( 'test', 'test', (object) array( 'status' => 400 ) );

$response = rest_convert_error_to_response( $error );
$this->assertInstanceOf( 'WP_REST_Response', $response );

// stdClass data should not cause a fatal, status should default to 500.
$this->assertSame( 500, $response->get_status() );
}

public function test_rest_error() {
$data = array(
'code' => 'wp-api-test-error',
Expand Down
Loading