BookStackApp/BookStack · error · Error
Attempted to use EditorUIContext before it has been set
Error message
Attempted to use EditorUIContext before it has been set
What it means
The WYSIWYG UI framework stores the EditorUiContext in a core singleton which starts as null. getContext() refuses to hand out an unset context because nearly every UI helper (toolbars, modals, translations via trans()) depends on it. Throwing here turns a silent null-dereference later into an immediate, diagnosable failure.
Source
Thrown at resources/js/wysiwyg/ui/framework/core.ts:43
export function isUiBuilderDefinition(object: any): object is EditorUiBuilderDefinition {
return 'build' in object;
}
export abstract class EditorUiElement {
protected dom: HTMLElement|null = null;
private context: EditorUiContext|null = null;
protected abortController: AbortController = new AbortController();
protected abstract buildDOM(): HTMLElement;
setContext(context: EditorUiContext): void {
this.context = context;
}
getContext(): EditorUiContext {
if (this.context === null) {
throw new Error('Attempted to use EditorUIContext before it has been set');
}
return this.context;
}
getDOMElement(): HTMLElement {
if (!this.dom) {
this.dom = this.buildDOM();
}
return this.dom;
}
rebuildDOM(): HTMLElement {
const newDOM = this.buildDOM();
this.dom?.replaceWith(newDOM);
this.dom = newDOM;
return this.dom;View on GitHub (pinned to 18f8469a1c)
Solutions
- Ensure setContext(editorUiContext) is called before any other UI framework usage — in the code that creates the editor UI.
- Check initialization order: any code calling getContext() (directly or via trans()) must run after the editor bootstrap completes, not at module import time.
- If rendering UI outside a real editor, construct a minimal EditorUiContext and pass it through setContext() first.
- Defer context-dependent work until the editor setup/bootstrap callback has run.
Example fix
// before
import { trans } from '../wysiwyg/ui/framework/core';
const label = trans('Bold'); // throws if editor not yet created
// after
// run inside the editor bootstrap callback, after setContext() has been called
const label = trans('Bold'); Defensive patterns
Strategy: validation
Validate before calling
// Assert editor UI bootstrapped before using context-dependent APIs
function assertUiReady(): void {
// flag set by the bootstrap right after setContext()
if (!window.__editorUiBootstrapped) {
throw new Error('Editor UI not initialized: setContext() has not run yet');
}
}
assertUiReady();
const ctx = getContext(); Type guard
function hasContext(core: { getContext(): EditorUiContext }): boolean {
try { return core.getContext() != null; } catch { return false; }
} Try / catch
try {
const ctx = getContext();
// use ctx
} catch (e) {
if (e instanceof Error && e.message.includes('before it has been set')) {
// bootstrap not finished: defer or initialize first
} else { throw e; }
} Prevention
- Call setContext() as the very first step of editor bootstrap, before any UI component renders.
- Never use trans()/getContext() at module import time — only inside functions invoked after setup.
- Expose an isReady/hasContext flag and gate UI code on it.
- In tests, provide a shared fixture that always sets a context before importing UI helpers.
When it happens
Trigger: Calling getContext() (directly or via trans(), modals, or any UI element) before setContext() has been called with a real EditorUiContext — e.g. rendering editor UI outside the editor bootstrap, or using a translate helper before the editor is created.
Common situations: Mounting WYSIWYG UI components in a page where the editor was never initialized (or init failed earlier), importing UI pieces into tests without a fixture context, calling UI code before the bootstrap completes, or an upgrade that moved context setup later in the boot sequence.
Related errors
- Context attempted to be used without being 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/3a00e237987b5dfd.
Report an issue: GitHub.