Make WordPress Core

Opened 4 years ago

Last modified 3 months ago

#58255 new enhancement

wp_send_json_error should respect error state passed in WP_Error.

Reported by: spacedmonkey Owned by:
Priority: normal Milestone: Awaiting Review
Component: Administration Version:
Severity: normal Keywords: good-first-bug has-patch
Cc: Focuses:

Description

WP_Error objects support setting the error state. This is used heavily in the REST API. The function wp_send_json_error supports error status code and passing WP_Error as an argument. The wp_send_json_error function could respect the status code in WP_Error.

Attachments (3)

0001-Set-status-code-when-it-is-null.patch​ (868 bytes ) - added by rtiodev 2 years ago.
Based on this ​https://github.com/WordPress/wordpress-develop/pull/4496 without the formatting stuff and fixed the conflicts.
good first bug 2.txt​ (246 bytes ) - added by vedantsonone1234 19 months ago.
58255-wp-send-json-error-respect-wp-error-status.patch​ (730 bytes ) - added by sachinrajcp123 9 months ago.

Download all attachments as: .zip

Change History (13)

#1 @erichmond
3 years ago

I'm happy to look at this, can I get some more context just so I can replicate it easily with out going down the wrong track?

I'm guessing this isn't just a simple ajax call and to replicate it and I'll need to trigger WP_Error through a REST API route?

#2 @rohitmathur7
3 years ago

I looked into this and this is my analysis:

Problem:

If we pass WP_Error as an argument in the wp_send_json_error() then the status code set in the WP_Error is not set as the status code in the wp_send_json_error().

Approach:

I am using this way to pass the WP_Error in the wp_send_json_error():

$error_message = 'new error';
$error         = new WP_Error( 'error_code', $error_message, array( 'status' => 401 ) );
return wp_send_json_error( $error );

After applying changes to the code I was able to get this response:
​https://drive.google.com/file/d/13HzEMQufbSzdqlQonLPXCGeultzW8bGy/view?usp=sharing

If this works I can provide the patch for this.

Please correct me if I am going in the wrong direction with this.

This ticket was mentioned in ​PR #4496 on ​WordPress/wordpress-develop by ​rohitmathur-7.


3 years ago
#3

  • Keywords has-patch added

Set the status code set in WP_Error as the status code if it is null.

  • If we pass WP_Error in the wp_send_json_error() function, then the status code set in the WP_Error object was not getting set as the status code.
  • So I am checking if the status code is set in the wp_send_json_error(), and If not then setting the status code of the WP_Error object as the status code in the wp_send_json_error().

Trac ticket: https://core.trac.wordpress.org/ticket/58255

#4 @spacedmonkey
3 years ago

Love the thoughts of @danielbachhuber on this one.

#5 follow-up: @danielbachhuber
3 years ago

I'm not opposed to it, but I'm not sure how much value it provides.

One backwards compat concern: If someone is already passing a WP_Error object into wp_send_json_error(), this would change the behavior.

Additionally, 'status' is a magical property of REST API errors. If we were to introduce this new behavior, we may want to formalize that in some way.

#6 in reply to: ↑ 5 @spacedmonkey
3 years ago

Replying to danielbachhuber:

I'm not opposed to it, but I'm not sure how much value it provides.

One backwards compat concern: If someone is already passing a WP_Error object into wp_send_json_error(), this would change the behavior.

Additionally, 'status' is a magical property of REST API errors. If we were to introduce this new behavior, we may want to formalize that in some way.

I am not sure what the best course is here. I think the change of behaviour is a good thing, as the correct error code is sent. I don't see why changing this error state would be a problem.

#7 in reply to: ↑ description @rohitsharma06
16 months ago

Just wondering if this is still in progress. If not, I’d be happy to continue from where it left off.

This ticket was mentioned in ​PR #9269 on ​WordPress/wordpress-develop by ​@vyasnaman.


16 months ago
#9

This pull request introduces an enhancement to the wp_send_json_error function in src/wp-includes/functions.php. The change ensures that if no $status_code is explicitly provided, the function attempts to derive it from the error data associated with the $value parameter.

Key change:

  • Enhanced the wp_send_json_error function to check if $status_code is null and, if so, retrieve the status code from the error data of $value (if available). This adds flexibility and ensures proper status code handling for error responses.

Trac ticket: https://core.trac.wordpress.org/ticket/58255

#10 @dhavalkapadane
3 months ago

Tested this by calling wp_send_json_error() with a WP_Error object containing 'status' => 401 in its error data, without passing an explicit $status_code, and checking the actual HTTP response status code via the browser's Network tab.

Without the patch:
HTTP status code returned: 200
The function ignores the 'status' value in the WP_Error's error data entirely and defaults to 200, even though the response body represents an error ("success":false).

With the patch applied (deriving $status_code from WP_Error::get_error_data() when not explicitly passed):
HTTP status code returned: 401
The patch works as intended — the response now correctly reflects the error status carried by the WP_Error object.

One general observation worth noting for the ticket: since this changes the actual HTTP status code returned by existing wp_send_json_error() calls that pass a WP_Error with 'status' data (previously always 200 unless explicitly overridden), any AJAX handlers or JS relying on a 200 response with a success:false body to detect errors would start receiving non-200 responses instead. Worth confirming this backward-compatibility angle is intentional/desired before merging.

Environment: WordPress trunk (wordpress-develop local env), PHP 8.3.33, Windows.

Note: See TracTickets for help on using tickets.