Opened 8 months ago
Closed 8 months ago
#64901 closed defect (bug) (fixed)
rest_convert_error_to_response() fatals when WP_Error data is a stdClass object in 7.0-beta5
| Reported by: | nateallen | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.0 |
| Component: | REST API | Version: | 7.0 |
| Severity: | critical | Keywords: | has-patch has-unit-tests commit |
| Cc: | Focuses: | rest-api |
Description
The rest_convert_error_to_response() function throws a PHP fatal error when a WP_Error object contains error data that is a stdClass instead of an array.
PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array in wp-includes/rest-api.php on line 3431
Cause
In 61429 (part of the Code Modernization effort, #63430), the isset() ternary in rest_convert_error_to_response() was replaced with a null coalescing operator:
Before (6.9.4):
<?php return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
After (7.0-beta5):
<?php return $error_data['status'] ?? $status;
The is_array() guard was critical here because WP_Error::get_all_error_data() can return data of any type, such as arrays, objects, strings, integers, etc. Removing it causes a fatal when error data is a stdClass.
Steps to reproduce
Register a REST route that returns a WP_Error with stdClass data:
<?php add_action( 'rest_api_init', function () { register_rest_route( 'test/v1', '/trigger-bug', array( 'methods' => 'GET', 'callback' => function () { return new WP_Error( 'test_error', 'Test error', (object) array( 'status' => 500 ) ); }, 'permission_callback' => '__return_true', ) ); } );
Then request GET /wp-json/test/v1/trigger-bug.
Expected result
A JSON error response (as in 6.9.4).
Actual result
PHP fatal error on line 3431 of wp-includes/rest-api.php.
Suggested fix
Restore the is_array() check:
<?php return is_array( $error_data ) ? ( $error_data['status'] ?? $status ) : $status;
Change History (9)
This ticket was mentioned in PR #11307 on WordPress/wordpress-develop by @nateallen.
8 months ago
#4
- Keywords has-patch has-unit-tests added
61429 (part of the Code Modernization effort, #63430) replaced the isset() ternary in rest_convert_error_to_response() with a null coalescing operator, removing the is_array() guard. Since WP_Error::get_all_error_data() can return data of any type, this causes a PHP fatal error when error data is a stdClass object.
This PR reverts the line to its original form and adds a test case for stdClass error data.
Trac ticket: https://core.trac.wordpress.org/ticket/64901
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.6
Used for: Identifying the regression by diffing 6.9.4 and 7.0-beta5, and writing the test case. The fix itself is a direct revert of the changed line. All code was reviewed by me.
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
8 months ago
@westonruter commented on PR #11307:
8 months ago
#7
Thank you for addressing this.
The prior change was indeed wrong because \WP_Error::get_all_error_data() returns mixed[]:
I was curious why I wasn't seeing PHPStan flag this issue locally since where I have the PHPStan rule level 8 enforced. Only when I switched to rule level 9 did PHPStan catch the issue because it will:
be strict about explicit
mixedtype - the only allowed operation you can do with it is to pass it to anothermixed
Once I did that, it was caught:
Better to revert.