Fatal error when "user" context does not implement AccountInterface

Created on 21 September 2022, about 2 years ago
Updated 13 October 2023, about 1 year ago

Problem/Motivation

A fatal error occurs when the context key "user" is present but this has a value which is anything other than an object that implements AccountInterface. It is not guaranteed that the "user" context key is an account object at all times. We should check the type beforehand.

Steps to reproduce

This can be easily reproduced by installing the Monolog module, and logging in. The Monolog module includes a CurrentUserProcessor which provides a "user" context key which is not a full user object but instead a string containing the user name.

The following error occurs:

Error: Call to a member function id() on string in Drupal\jsonlog\Logger\JsonLogData->setAccount() (line 167 of /app/docroot/modules/contrib/jsonlog/src/Logger/JsonLogData.php).

Drupal\jsonlog\Logger\JsonLogData->setAccount('admin@migros.dummy') (Line: 192)
Drupal\jsonlog\Logger\JsonLog->prepareLog(3, 'Error: Call to a member function id() on string in Drupal\jsonlog\Logger\JsonLogData->setAccount() (line 167 of /app/docroot/modules/contrib/jsonlog/src/Logger/JsonLogData.php) #0 /app/docroot/modules/contrib/jsonlog/src/Logger/JsonLog.php(192): Drupal\jsonlog\Logger\JsonLogData->setAccount('admin@migros.du...')
#1 /app/docroot/modules/contrib/jsonlog/src/Logger/JsonLog.php(125): Drupal\jsonlog\Logger\JsonLog->prepareLog(5, 'Session opened ...', Array)
#2 /app/docroot/modules/contrib/monolog/src/Logger/Handler/DrupalHandler.php(60): Drupal\jsonlog\Logger\JsonLog->log(5, 'Session opened ...', Array)
#3 /app/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(48): Drupal\monolog\Logger\Handler\DrupalHandler->write(Array)
#4 /app/vendor/monolog/monolog/src/Monolog/Logger.php(327): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#5 /app/docroot/modules/contrib/monolog/src/Logger/Logger.php(46): Monolog\Logger->addRecord(250, 'Session opened ...', Array)
#6 /app/vendor/monolog/monolog/src/Monolog/Logger.php(539): Drupal\monolog\Logger\Logger->addRecord(250, 'Session opened ...', Array)
#7 /app/docroot/core/modules/user/user.module(456): Monolog\Logger->notice('Session opened ...', Array)

Proposed resolution

Check that the data is of the correct type before passing it to the method.

🐛 Bug report
Status

Closed: won't fix

Version

3.0

Component

Code

Created by

🇧🇬Bulgaria pfrenssen Sofia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024