Make WordPress Core

Opened 17 months ago

Last modified 15 months 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 (1)

0001-Set-status-code-when-it-is-null.patch (868 bytes) - added by rtiodev 5 weeks ago.
Based on this https://github.com/WordPress/wordpress-develop/pull/4496 without the formatting stuff and fixed the conflicts.

Download all attachments as: .zip

Change History (7)

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


16 months 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
16 months ago

Love the thoughts of @danielbachhuber on this one.

#5 follow-up: @danielbachhuber
16 months 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
15 months 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
5 weeks ago

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

Note: See TracTickets for help on using tickets.