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
Conversation
php/class-wp-cli.php
Outdated
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I prefixed the error message with a class name.
php/class-wp-cli.php
Outdated
@@ -1004,6 +1004,10 @@ public static function error_to_string( $errors ) { | |||
return $message; | |||
} | |||
} | |||
|
|||
if ( $errors instanceof \Exception ) { |
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 :)
Hi @schlessera, sorry it took me so long - I've made the changes as requested/suggested. |
WP_CLI::error_to_string()
method
Thanks for the PR, @chesio ! |
Created new issue #5356 as a result of seeing the implementation here. |
Fixes #5267.
Note: I don't know if returning $e->getMessage() or converting exception to string is preferred. PR does the former.