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
- Initialize the manager by setting its context (the setup path that wires context.editor) before touching manager APIs.
- Defer manager-dependent code (setToolbar, createModal, triggerStateUpdateForElement) until after editor bootstrap completes.
- In tests or custom integrations, construct a full EditorUiContext and assign it first.
- 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
- Run all manager API calls inside the editor bootstrap/setup flow, never at module scope.
- Keep a single ownership point for calling the manager's context setup.
- In tests, build a full EditorUiContext fixture before instantiating manager-dependent objects.
- Queue manager-dependent work (toolbars, modals) until setup has signaled completion.
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
- Attempted to use EditorUIContext before it has been set
- Attempted to get use node without it being set
- Can't find options for dropdown menu
- Attempted to show modal of key [${key}] but no modal registe
AI-assisted analysis of BookStackApp/BookStack@18f8469a1c (2026-09-02).
Data as JSON: /api/errors/35c403a849e8db9b.
Report an issue: GitHub.