BookStackApp/BookStack · error · Error

Context attempted to be used without being set

Error message

Context attempted to be used without being set

What it means

The EditorUiManager holds the shared EditorUiContext, initially null, and is configured via the setup path that also wires the editor and dropdown RTL state. getContext() throws if accessed before configuration, since every manager feature (modals, toolbars, state refresh, decorators) requires the context. This fails fast instead of cascading null errors.

Source

Thrown at resources/js/wysiwyg/ui/framework/manager.ts:38

    protected decoratorInstancesByNodeKey: Record<string, EditorDecorator> = {};
    protected context: EditorUiContext|null = null;
    protected toolbar: EditorContainerUiElement|null = null;
    protected contextToolbarDefinitionsByKey: Record<string, EditorContextToolbarDefinition> = {};
    protected activeContextToolbars: EditorContextToolbar[] = [];
    protected selectionChangeHandlers: Set<SelectionChangeHandler> = new Set();
    protected domEventAbortController = new AbortController();
    protected teardownCallbacks: (()=>void)[] = [];

    setContext(context: EditorUiContext) {
        this.context = context;
        this.setupEventListeners();
        this.setupEditor(context.editor, context);
        this.dropdowns.setIsRTL(this.context.manager.getDefaultDirection() === 'rtl');
    }

    getContext(): EditorUiContext {
        if (this.context === null) {
            throw new Error(`Context attempted to be used without being set`);
        }

        return this.context;
    }

    triggerStateUpdateForElement(element: EditorUiElement) {
        element.updateState({
            selection: null,
            editor: this.getContext().editor
        });
    }

    registerModal(key: string, modalDefinition: EditorFormModalDefinition) {
        this.modalDefinitionsByKey[key] = modalDefinition;
    }

    createModal(key: string): EditorFormModal {
        const modalDefinition = this.modalDefinitionsByKey[key];

View on GitHub (pinned to 18f8469a1c)

Solutions

  1. Initialize the manager by setting its context (the setup path that wires context.editor) before touching manager APIs.
  2. Defer manager-dependent code (setToolbar, createModal, triggerStateUpdateForElement) until after editor bootstrap completes.
  3. In tests or custom integrations, construct a full EditorUiContext and assign it first.
  4. Gate manager access on an is-configured check and queue work until setup runs.

Example fix

// before
const manager = ui.getManager();
manager.setToolbar(toolbar); // throws: context not yet set

// after
// run after editor bootstrap has configured the manager
const manager = ui.getManager();
manager.setToolbar(toolbar); // safe
Defensive patterns

Strategy: validation

Validate before calling

// Gate manager usage on manager setup having completed
function managerIsConfigured(manager: EditorUiManager): boolean {
    try {
        manager.getContext();
        return true;
    } catch {
        return false;
    }
}
if (managerIsConfigured(manager)) {
    manager.setToolbar(toolbar);
}

Type guard

function withContext<T>(manager: EditorUiManager, fn: (ctx: EditorUiContext) => T): T | null {
    try { return fn(manager.getContext()); } catch { return null; }
}

Try / catch

try {
    manager.setToolbar(toolbar);
} catch (e) {
    if (e instanceof Error && e.message.includes('without being set')) {
        // manager not configured yet: run this after editor bootstrap instead
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling getContext() — directly or indirectly via triggerStateUpdateForElement(), createModal(), decorator(), setToolbar(), the editor accessor, or triggerFutureStateRefresh() — before the manager has been given an EditorUiContext.

Common situations: Accessing manager.editor or manager.decorator(...) at module scope before the editor UI is bootstrapped; building toolbar/modal objects where editor init was skipped or failed; tests instantiating the manager without a context fixture.

Related errors


AI-assisted analysis of BookStackApp/BookStack@18f8469a1c (2026-09-02). Data as JSON: /api/errors/35c403a849e8db9b. Report an issue: GitHub.