Support exceptions in `WP_CLI::error_to_string()` method by chesio · Pull Request #5331 · wp-cli/wp-cli

@chesio

Fixes #5267.

Note: I don't know if returning $e->getMessage() or converting exception to string is preferred. PR does the former.

@chesio

schlessera

}

if ( $errors instanceof \Exception ) {
return $errors->getMessage();

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.

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.

}
}

if ( $errors instanceof \Exception ) {

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

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

@chesio

@chesio

@chesio

Hi @schlessera, sorry it took me so long - I've made the changes as requested/suggested.

schlessera

@schlessera schlessera changed the title Support exceptions in WP_CLI::error_to_string method Support exceptions in WP_CLI::error_to_string() method

Feb 18, 2020

@schlessera

@schlessera

Created new issue #5356 as a result of seeing the implementation here.