[wp-trac] [WordPress Trac] #57842: Add a new independent WP_Exception class, compatible with WP_Error

WordPress Trac noreply at wordpress.org
Wed Mar 1 22:05:19 UTC 2023


#57842: Add a new independent WP_Exception class, compatible with WP_Error
-------------------------+-----------------------------
 Reporter:  junaidbhura  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:  has-patch
  Focuses:               |
-------------------------+-----------------------------
 Exceptions offer a better way of working with errors. Whilst we all
 appreciate WP_Error and how deeply integrated into WordPress it is, we
 should at least start to think about introducing an independent and
 official `WP_Exception` class.

 I don't think I will need to elaborate the benefits Exceptions bring into
 modern development on this ticket.

 Instead, perhaps we could use this ticket to discuss a way to officially
 introduce an Exception as a starting point.

 Once this is accepted, we could then discuss the pros and cons of updating
 existing code to use this new exception in other tickets. But we should,
 at the very least, offer a standard Exception for developers to start
 implementing in their custom code bases and plugins.

 I've created a super simple WP_Exception class which is compatible with
 WP_Error as a starting point. Happy to have a serious discussion on how
 best to improve this and get something like this merged into core.

 Sample usage:

 {{{
 // Normal Exception.
 try {
         throw new WP_Exception( 'error_code', 'Error message', $error_data
 );
 } catch ( WP_Exception $e ) {
         $e->get_error_code();
         $e->get_error_message();
         $e->get_error_data();
 }

 // To WP Error.
 try {
         my_function();
 } catch ( WP_Exception $e ) {
         return $e->to_wp_error(); // Returns a WP_Error based on the
 exception.
 }

 // From WP Error.
 if ( is_wp_error( $thing ) ) {
         $exception = new WP_Exception();
         throw $exception->from_wp_error( $thing );
 }
 }}}

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


More information about the wp-trac mailing list