Make WordPress Core

Opened 2 years ago

Last modified 3 days ago

#58255 new enhancement

wp_send_json_error should respect error state passed in WP_Error.

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

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 (2)

0001-Set-status-code-when-it-is-null.patch (868 bytes) - added by rtiodev 11 months 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 3 months ago.

Download all attachments as: .zip

Change History (11)

#1 @erichmond
2 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
2 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.


2 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
2 years ago

Love the thoughts of @danielbachhuber on this one.

#5 follow-up: @danielbachhuber
2 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
2 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.

@rtiodev
11 months ago

Based on this https://github.com/WordPress/wordpress-develop/pull/4496 without the formatting stuff and fixed the conflicts.

#7 in reply to: ↑ description @rohitsharma06
2 weeks 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.


3 days 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

Note: See TracTickets for help on using tickets.