Could not prepare context: mgr

Hello,
I use MODX 3.1.0-pl on php 8.4.2 and NGINX 1.27.3
in my error log i get:

[2025-01-02 12:42:26] (ERROR @ /var/www/amtis/web/core/src/Revolution/modX.php : 2590) Could not prepare context: mgr

Can you tell me how I can fix it?
thanks in advance

When exactly does this error occur? When you try to log in to the MODX manager? Or during a certain action while using the MODX manager?

Is the MODX manager still usable?
Is this a fresh installation of MODX or an upgrade from an older version?

Hello dear halftrainedharry,
this error started appearing after updating to the latest version of MODX.
it appears when i surf in any of contexts as well as the main one. I have over 10 context / subdomains contexts settings is like:


and i have SubfolderContextMapRouter plugin:

<?php

error_reporting(0);
if ($modx->context->get('key') !== 'mgr') {

    $contexts = array();

    $cacheKey = $modx->getOption('cache_context_map_key', $scriptProperties, 'context_map');
    $cacheOptions = array(
        xPDO::OPT_CACHE_HANDLER => $modx->getOption("cache_{$cacheKey}_handler", $scriptProperties, $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
        xPDO::OPT_CACHE_EXPIRES => $modx->getOption("cache_{$cacheKey}_expires", $scriptProperties, $modx->getOption(xPDO::OPT_CACHE_EXPIRES)),
    );
    /** @var xPDOCache $contextCache */
    $contextCache = $modx->cacheManager->getCacheProvider($cacheKey, $cacheOptions);

    if ($contextCache) {
        $contexts = $contextCache->get('context_map');
    }

    if (empty($contexts)) {
        /** @var modContext $contextsGraph */
        $query = $modx->newQuery('modContext');
        $query->where(array('modContext.key:NOT IN' => array('web', 'mgr')));
        $query->sortby($modx->escape('modContext') . '.' . $modx->escape('key'), 'ASC');
        $contextsGraph = $modx->getCollectionGraph('modContext', '{"ContextSettings":{}}', $query);
        foreach ($contextsGraph as $context) {
            $contextSettings = array();
            foreach ($context->ContextSettings as $cSetting) {
                $contextSettings[$cSetting->get('key')] = $cSetting->get('value');
            }
            $contexts[$context->get('key')] = $contextSettings;
        }
        unset($contextsGraph);
        if ($contextCache) {
            $contextCache->set('context_map', $contexts);
        }
    }

    if (!empty($contexts)) {
        $pieces = explode('/', trim($_REQUEST[$modx->getOption('request_param_alias', null, 'q')], ' '), 2);
        if (count($pieces) > 0) {
            foreach($contexts as $cKey => $cSettings) {
                if ($pieces[0] == $cKey) {
                    if (isset($pieces[1])) {
                        $_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = $pieces[1];
                    } else {
                        $modx->sendRedirect(MODX_SITE_URL . $pieces[0] . '/', array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
                    }
                    $modx->switchContext($cKey);
                    $modx->log(modX::LOG_LEVEL_INFO, "Switched to context {$cKey} from URI {$_REQUEST['q']}");
                    break;
                }
            }
        }
    }
}

YES → MODX manager still usable
NO → this is upgrade from 2x to latest version of the MODX 3
Thank you!

It’s unclear, when exactly this error message is created.
It’s thrown here in the code in the function _initContext().

The function _initContext() is called on every request to initialize the context and after a context switch.


The line $modx->switchContext($cKey); will call the _initContext() function again. But as the context “mgr” is explicitely excluded in the code ($query->where(array('modContext.key:NOT IN' => array('web', 'mgr')));), it’s unlikely that this plugin is the source of the error.

halftrainedharry,
How can I find out exactly when and why this error occurs?

The best way is to use a debugger like Xdebug.
But this requires some experience with reading and debugging code.


Alternatively you could try logging additional information to the MODX error log. This may help finding the cause of the issue.

For example you could (temporarily) add the line
$this->log(modX::LOG_LEVEL_ERROR, 'Stack trace: ' . (new \Error)->getTraceAsString());
right after the line that logs the 'Could not prepare context...' error message.
This logs the stack trace and tells you more about the series of function calls that leads to the error.