Skip to content
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

Merged
merged 3 commits into from Feb 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions php/class-wp-cli.php
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1004,6 +1004,10 @@ public static function error_to_string( $errors ) {
return $message;
}
}

if ( $errors instanceof \Exception ) {
Copy link
Member

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+.

Copy link
Contributor Author

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 :)

return $errors->getMessage();
Copy link
Member

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.

Copy link
Contributor Author

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.

}
}

/**
Expand Down