Skip to content

Commit

Permalink
Merge pull request #5331 from chesio/issue-5267
Browse files Browse the repository at this point in the history
Support exceptions in `WP_CLI::error_to_string()` method
  • Loading branch information
schlessera committed Feb 18, 2020
2 parents 48c610c + 693dc22 commit 6f5e81f
Showing 1 changed file with 9 additions and 3 deletions.
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

0 comments on commit 6f5e81f

Please sign in to comment.