Opened 19 months ago
Last modified 19 months ago
#57842 new enhancement
Add a new independent WP_Exception class, compatible with WP_Error
Reported by: | junaidbhura | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch |
Focuses: | Cc: |
Description
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 ); }
Trac ticket: https://core.trac.wordpress.org/ticket/57842