-
Notifications
You must be signed in to change notification settings - Fork 956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support exceptions in WP_CLI::error_to_string()
method
#5331
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -825,8 +825,8 @@ public static function warning( $message ) { | |
* @access public | ||
* @category Output | ||
* | ||
* @param string|WP_Error $message Message to write to STDERR. | ||
* @param boolean|integer $exit True defaults to exit(1). | ||
* @param string|WP_Error|Exception $message Message to write to STDERR. | ||
* @param boolean|integer $exit True defaults to exit(1). | ||
* @return null | ||
*/ | ||
public static function error( $message, $exit = true ) { | ||
|
@@ -976,7 +976,7 @@ public static function print_value( $value, $assoc_args = array() ) { | |
} | ||
|
||
/** | ||
* Convert a wp_error into a string | ||
* Convert a WP_Error or Exception into a string | ||
* | ||
* @param mixed $errors | ||
* @return string | ||
|
@@ -1004,6 +1004,10 @@ public static function error_to_string( $errors ) { | |
return $message; | ||
} | ||
} | ||
|
||
if ( $errors instanceof \Exception ) { | ||
return $errors->getMessage(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to also print the concrete class of the exception, as that is often the most important distinction amongst exceptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefixed the error message with a class name. |
||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for
Throwable
as well, as that is the base interface to catch starting with PHP 7+.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, I learned something new :)