[wp-trac] [WordPress Trac] #40580: rest_ensure_response() can return WP_Error object, but is consequently not checked in get_item() method

WordPress Trac noreply at wordpress.org
Thu Apr 27 19:39:44 UTC 2017


#40580: rest_ensure_response() can return WP_Error object, but is consequently not
checked in get_item() method
-------------------------------------------------+-------------------------
 Reporter:  ruud@…                               |       Owner:
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
Component:  REST API                             |  Review
 Severity:  normal                               |     Version:  4.7
 Keywords:  has-patch dev-feedback needs-unit-   |  Resolution:
  tests                                          |     Focuses:
-------------------------------------------------+-------------------------

Comment (by ruud@…):

 Hi James,

 Thanks for getting back to me.

 >Under what situations does rest_ensure_response return a WP_Error?

 Well, in my case I used the rest_prepare_{$this->post_type} filter to
 check if this post is allowed to be served via the REST API. I'm checking
 a custom metakey value for this.

 Once this check fails it generates the WP_Error which is then passed into
 rest_ensure_response() and then rest_unsure_response() just returns the
 WP_Error as well.

 >This looks like a good thing to fix, one way or another
 Yes, but an early return might also be a (better?) option for this.

 >but this change does need unit tests.
 Agreed. I have limited experience with unit tests, but have made a few for
 WP before.
 You mean like creating a new method, expanding on 'test_get_item' and
 adding in an add_filter which returns a WP_Error, and then
 $this->assertInstanceOf( 'WP_Error', $response ); ?

 I can see many examples of asserts for the $response not being an
 WP_Error, however this can be the case as well ($response being a WP_Error
 object), so am I on the right track here?

 Thanks,
 Ruud

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40580#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list