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?

  • 4
    @leymannx I think this is a fair question. In addition to helping out people who aren't so good with code, having questions like this show up in Google searches makes it easier for everyone to update their code quickly. – Patrick Kenny Jul 23 '20 at 6:05
  • @PatrickKenny – I totally agree with you. It's just I don't like one sentence questions not showing minimal effort to solve a problem on their own. Like: What have you tried so far etc. There are probably a thousand D9 ready contrib modules out there, maybe 25% of them providing their own Drush commands. Devel foremost. Looking a bit around shouldn't be too high of a hurdle. Hitting Google for that exact function brings up weitzman.github.io/blog/port-to-drush9 with an answer to that question. – leymannx Jul 23 '20 at 6:51
  • 2
    I am glad I asked this question as now the exact answer to the search phrase comes up in the Google results. I know a lot of people who have a lot of anxiety when reaching out and asking for help because of the condescending and often rude responses they get back. It's great that you are helping people on here @leymannx but you should think about how you phrase such replies. You might not realise it but you can have a drastic effects on other people's confidence, self esteem and mental health. – John Oct 2 '20 at 8:54
  • 2
    You are right, and I apologise, I should have phrased my initial comment differently. 💙 – leymannx Oct 2 '20 at 10:10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.