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 all commits
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
12 changes: 9 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,12 @@ public static function error_to_string( $errors ) {
return $message;
}
}

// PHP 7+: internal and user exceptions must implement Throwable interface.
// PHP 5: internal and user exceptions must extend Exception class.
if ( interface_exists( 'Throwable' ) && ( $errors instanceof \Throwable ) || ( $errors instanceof \Exception ) ) {
return get_class( $errors ) . ': ' . $errors->getMessage();
}
}

/**
Expand Down