TransWikia.com

How do I replace drush_set_error()?

Drupal Answers Asked on November 28, 2021

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?

One Answer

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.

Replace drush_set_error() with throw new Exception()


Now let's compare a sample command in Drush 8 and in Drush 10 – the xkcd command.

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.'));

One more.

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 Drush::logger()->error().

Now what's the difference between Drush::logger()->error() and 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.

Answered by leymannx on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP