Skip to content
Closed
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
11 changes: 9 additions & 2 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3427,8 +3427,15 @@ function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C
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;
/**
* @param int $status Status.
* @param mixed $error_data Error data.
*/
static function ( int $status, $error_data ): int {
if ( is_array( $error_data ) && isset( $error_data['status'] ) && is_numeric( $error_data['status'] ) ) {
$status = (int) $error_data['status'];
}
return $status;
},
500
);
Expand Down
27 changes: 27 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,33 @@ 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::class, $response );

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

/**
* @ticket 64901
*/
public function test_error_to_response_with_multi_status_non_numeric_status() {
$error = new WP_Error( 'test', 'test', array( 'status' => array( 'feeling' => 'happy' ) ) );
$error->add_data( array( 'status' => 400 ), 'test' );
$error->add_data( array( 'status' => array( 'feeling' => 'bleh' ) ), 'test' );

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

$this->assertSame( 400, $response->get_status() );
}

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