[wp-trac] [WordPress Trac] #34999: Calling wp_die() in a REST API request should return valid JSON

WordPress Trac noreply at wordpress.org
Fri Dec 11 00:21:44 UTC 2015


#34999: Calling wp_die() in a REST API request should return valid JSON
-------------------------+-----------------------------
 Reporter:  joehoyle     |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  REST API     |    Version:  4.4
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 See https://github.com/WP-API/WP-API/issues/790, whenever `wp_die()` is
 used in an API Request (which ideally it should not, but it can still
 happen) we should return a valid JSON response with the error.

 I've been working on this, and started with an initial patch which just
 output the JSON and called exit, however this circumvents the rest of the
 API's routing etc _post_ die as script execution must stop at that point.

 I ended up using PHP exceptions, which I know it not that common within
 WordPress, however this is the only way we can stop PHP execution of the
 scope that call `wp_die()` and still have the REST API handle the response
 as usual, as if the endpoint returned a WP_Error, essentially.

 Attached a patch that implements this, though right now is a hack to
 checking if wp_die() caused the exception, this should use a
 `WP_Die_Exception` class if we actually decide to do this.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34999>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list