Problem/Motivation

If an error happens during the REST call, the module tries to display a message about the error to the user and crashes because of a wrong call to ->mesenger.

Error:


The website encountered an unexpected error. Try again later. Error: Call to a member function addError() on null in Drupal\azure_mailer\Plugin\Mail\AzureMailer->mail() (line 129 of modules/contrib/azure_mailer/src/Plugin/Mail/AzureMailer.php). Drupal\mailsystem\Adapter->mail() (Line: 308) Drupal\Core\Mail\MailManager->doMail() (Line: 181) Drupal\Core\Mail\MailManager->Drupal\Core\Mail\{closure}() (Line: 637) Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 180) Drupal\Core\Mail\MailManager->mail() (Line: 70) Drupal\mailsystem\MailsystemManager->mail() (Line: 109) Drupal\mail_debugger\Form\MailDebuggerForm->submitForm() call_user_func_array() (Line: 129) Drupal\Core\Form\FormSubmitter->executeSubmitHandlers() (Line: 67) Drupal\Core\Form\FormSubmitter->doSubmitForm() (Line: 597) Drupal\Core\Form\FormBuilder->processForm() (Line: 326) Drupal\Core\Form\FormBuilder->buildForm() (Line: 73) Drupal\Core\Controller\FormController->getContentResult() call_user_func_array() (Line: 123) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 637) Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181) Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76) Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53) Drupal\Core\StackMiddleware\Session->handle() (Line: 48) Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28) Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 116) Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90) Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36) Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51) Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741) Drupal\Core\DrupalKernel->handle() (Line: 19)

Steps to reproduce

Use a wrong Azure URL and try to send a mail.

Proposed resolution

Change:

$this->messenger->addError('Azure Communication Services error: ' . $e->getMessage());

To:

$this->messenger()->addError('Azure Communication Services error: ' . $e->getMessage());

in: https://git.drupalcode.org/project/azure_mailer/-/blob/2.0.x/src/Plugin/...

Explanation:

The issue is you’re calling the property $this->messenger, which is null unless you explicitly inject it. MessengerTrait only guarantees the method messenger() that lazy-loads the service.

Comments

marcusx created an issue. See original summary.

marcusx’s picture

Patch...

smustgrave’s picture

Status: Active » Reviewed & tested by the community

patch seems straight forward and that is correct for using the MessengerTrait.

jeffschuler’s picture

Patch working here. Thanks!

  • smustgrave committed f6645bae on 2.0.x
    fix: #3541803 Error: Call to a member function addError() on null in...
smustgrave’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.