[wp-trac] [WordPress Trac] #57842: Add a new independent WP_Exception class, compatible with WP_Error
WordPress Trac
noreply at wordpress.org
Tue Aug 5 22:58:30 UTC 2025
#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 | Resolution:
Keywords: has-patch | Focuses:
-------------------------+------------------------------
Comment (by junaidbhura):
Thanks for your response @dmsnell and @johnbillion !
Thanks for pointing out that I should probably elaborate what I mean,
instead of assuming what I write will make sense to everyone!
I should make this clarification: I am **NOT** proposing to replace
`WP_Error`. It has its place, and has very deep integration with WordPress
core.
What I am proposing is to create a **NEW** `WP_Exception` class - which
will be an official WordPress exception, which developers can start using,
which will help **aid** in working with `WP_Error`
@dmsnell Allow me to respond to your questions:
---
> Why is this preferred to ValueError, TypeError, Error, Exception? In
other words, if the proposal is to push WordPress code towards Exceptions,
what value does a custom exception bring to the table? Can you share some
code examples that highlight it?
The proposal is not to push WordPress core towards Exceptions, but rather
to provide an additional way for developers to handle errors / exceptions.
I will be adding some sample code after these questions to elaborate this.
---
> What is the functional goal of transforming into and out of WP_Error? Is
it to provide a smooth transition, where a function throws and then a
calling function returns the transformed value?
Again, the proposal is not to replace `WP_Error`. Since WordPress core is
reliant on `WP_Error`, and easy to use Exception to convert it into a
`WP_Error` will be beneficial. We can consider firing a hook for example
`do_action( 'wp_exception', $this )` every time an exception is triggered,
which can be beneficial for logging, etc. Or anything else that we may
require in the future, it keeps things scalable, and will provide an
"official" way for developers to throw exceptions. Example `class
MyException extends WP_Exception`. Right now different plugin vendors use
their own Exception classes.
---
> Did you consider adjusting WP_Error to extend Exception itself rather
than creating a new WP_Exception? What would it look like in comparison if
we could simply return or throw a WP_Error?
This may not be a bad idea, and something we can explore.
---
Let me elaborate a use case of using `WP_Exception` here:
Consider the following example, where a function returns a `WP_Error`
which needs to be used by a REST API endpoint:
{{{#!php
<?php
// Without WP_Exception.
function rest_api_handler(): array|WP_Error {
$value_1 = heavy_sub_function_1();
if ( $value_1 instanceof WP_Error ) {
do_action( 'my_custom_action' );
return $value_1;
}
$value_2 = heavy_sub_function_2();
if ( $value_2 instanceof WP_Error ) {
do_action( 'my_custom_action' );
return $value_2;
}
$value_3 = heavy_sub_function_3();
if ( $value_3 instanceof WP_Error ) {
do_action( 'my_custom_action' );
return $value_3;
}
if ( $some_risky_action ) {
do_action( 'my_custom_action' );
return new WP_Error( 'error_code', $data );
}
return $value;
}
}}}
{{{#!php
<?php
// With WP Exception.
function rest_api_handler(): array|WP_Error {
try {
$value_1 = heavy_sub_function_1(); // Throws WP_Exception
$value_2 = heavy_sub_function_2(); // Throws WP_Exception
$value_3 = heavy_sub_function_3(); // Throws WP_Exception
if ( $some_risky_action ) {
throw new WP_Exception( 'error_code', $data );
}
} catch ( WP_Exception $e ) {
do_action( 'my_custom_action' );
return $e->to_wp_error();
}
return $value;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/57842#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list