Just updating a module for Drupal 9 and Drush 10 and
drush_set_error() no longer appears as a function in the Drush code (as far as I can see). What should I be using instead?
drush_set_error() has basically been deprecated in Drush 8 and has been removed in Drush 10. Hitting up Google for drush_set_error() will give you a blog post by one of the maintainers: Porting Commands to Drush 9. In there the following is stated.
throw new \Exception()
Now let's compare a sample command in Drush 8 and in Drush 10 – the
In Drush 8 there's the following line:
drush_set_error('DRUSH_XKCD_SEARCH_FAIL', dt('The search failed or produced no results.'));
In Drush 10 this line became:
throw new \Exception(dt('The search failed or produced no results.'));
When you search the pull requests for drush_set_error() you'll find the following: Replace drush_set_error() and drush_log() via Drush::logger() #3810. In there
drush_set_error() has been replaced by
Now what's the difference between
throw new \Exception()?
throw new \Exception(dt('My error messsage.'));is the last thing you do before Drush quits.
Drush::logger()->error(dt('My error message'));gives you the chance to print out multiple error messages and let your code continue.