6

I have an indexer, which needs to be configured first - requires API credentials.

When I run in a reindex command in CLI: $ bin/magento indexer:reindex my_reindexer_name and the API credentials are not set, I want to display an error / warning message telling the user to configure his credentials. Currently I'm doing it like this:

if ($wrongCredentials) {
    $errorMessage = 'You need to configure your credentials';

    if (php_sapi_name() === 'cli') {
        throw new \Exception($errorMessage);
    }

    $this->messageManager->addErrorMessage($errorMessage);

    return;
}

The issue with this approach is that when the Magento is installed from CLI (via $ bin/magento setup:install ...), the Magento tries to reindex the data, the Exception is thrown (of course, API credentials are not set yet) - so the installation crashes. But I don't want to break the install, simply displaying the message is fine.

When I replace throw new \Exception(...); by simple echo $errorMessage (which works), Magento Marketplace refuses the extension from technical review (bad echo placement).

When I remove the CLI condition completely, $messageManager won't display the error message in CLI.

So the question is - how correctly display the error message in CLI without breaking the installation? Thanks!

1 Answer 1

6

If you are inside the execute method why don't you put the error message inside $output->writeln($errorMessage);? Where you have access to the OutputInterface

Update:

Inject \Symfony\Component\Console\Output\ConsoleOutput $output via constructor to access $this->output->writeln($errorMessage); inside your method.

8
  • Thanks for the advise, however in the indexer I don't have access to OutputInterface. The "reindex" command doesn't forward it to the indexers itself - github.com/magento/magento2/blob/… And OutputInterface cannot be instantiated via DI either. Am I missing something?
    – Jan Petr
    Jan 29, 2017 at 8:03
  • hi again, sorry, i misunderstood, you are referring to your Recurring.php right? could you show code of whole class if so?
    – belfort1
    Feb 1, 2017 at 12:16
  • Hey, not Recurring.php, but Indexer class. Here's the code of the whole class: github.com/algolia/algoliasearch-magento-2/blob/master/Model/… The critical lines are 46-48 with the exception. I want to remove the "throw Exception..." and replace it by something which will display the error message in CLI while reindexing with not-filled credentials.
    – Jan Petr
    Feb 2, 2017 at 8:53
  • Is it a requirement that it be configured with right credentials before doing reindex? Otherwise an option could be to prefill those fields through config.xml with some dummy data, if it is all that is required for reindexing not to throw an exception.
    – belfort1
    Feb 6, 2017 at 9:13
  • Yes, it's required for reindexing to have the right credentials. If the credentials are not right, the reindexing fails as the authorization fails (bad credentials ...). I just want to display a message to a user that the credentials are not filled and he should fill them and skip the reindexing in that case.
    – Jan Petr
    Feb 6, 2017 at 9:31

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.