{"record":{"id":"3cfe03cbae9991c0","repo":"siyuan-note/siyuan","slug":"globalthis-siyuan-event-is-s","errorCode":null,"errorMessage":"globalThis.siyuan.event is %s","messagePattern":"globalThis\\.siyuan\\.event is (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/plugin/sandbox.go","lineNumber":341,"sourceCode":"\n// dispatchEvent calls the globalThis.siyuan.event.on hook with the given event object.\nfunc dispatchEvent(p *KernelPlugin, rt *goja.Runtime, e any) (async bool, err error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\terr = fmt.Errorf(\"goja panic during dispatchEvent: %v\", r)\n\t\t}\n\t}()\n\n\tevent, err := getJsContextValue(rt, []any{\"siyuan\", \"event\"})\n\tif err != nil {\n\t\treturn\n\t}\n\tif event == nil {\n\t\terr = fmt.Errorf(\"globalThis.siyuan.event not found\")\n\t\treturn\n\t}\n\tif goja.IsUndefined(event) || goja.IsNull(event) {\n\t\terr = fmt.Errorf(\"globalThis.siyuan.event is %s\", event.String())\n\t\treturn\n\t}\n\n\teventObj := event.ToObject(rt)\n\tif eventObj == nil {\n\t\terr = fmt.Errorf(\"globalThis.siyuan.event is not an object\")\n\t\treturn\n\t}\n\n\thandlerValue := eventObj.Get(\"handler\")\n\thandler, ok := goja.AssertFunction(handlerValue)\n\tif !ok {\n\t\treturn\n\t}\n\n\teventJs := rt.ToValue(e)\n\tinvokeResult, invokeErr := handler(event, eventJs)\n\tif invokeErr != nil {","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/plugin/sandbox.go#L323-L359","documentation":"After resolving globalThis.siyuan.event, dispatchEvent checks it is neither undefined nor null. If the property exists but holds undefined/null, this error reports the actual value so the developer knows the event hub binding is empty.","triggerScenarios":"Plugin or bootstrap code assigned undefined/null to globalThis.siyuan.event; siyuan module injection created the siyuan object but failed to attach event; a JS script reset the namespace before an event was dispatched.","commonSituations":"Partially failed EnableSiyuanModule where injection aborted after creating the siyuan object; plugin scripts that clear globals; event dispatched before sandbox setup finished.","solutions":["Ensure the siyuan module injection completes (event hub registered) before dispatching","Check the reported value in the message to confirm it is undefined vs null","Fix plugin/global code that assigns undefined or null to siyuan.event","Re-run sandbox bootstrap and verify injection errors from earlier steps"],"exampleFix":"// before\nrt.RunScript(\"setup\", \"globalThis.siyuan.event = undefined\")\ndispatchEvent(p, rt, \"someEvent\", data)\n// after\nrt.RunScript(\"setup\", \"\") // do not clobber siyuan.event\ndispatchEvent(p, rt, \"someEvent\", data)","handlingStrategy":"type-guard","validationCode":"event, _ := getJsContextValue(rt, []any{\"siyuan\", \"event\"})\nif goja.IsUndefined(event) || goja.IsNull(event) {\n    return fmt.Errorf(\"event hub not initialized\")\n}","typeGuard":"func hasEventHub(rt *goja.Runtime) bool {\n    v, err := getJsContextValue(rt, []any{\"siyuan\", \"event\"})\n    return err == nil && v != nil && !goja.IsUndefined(v) && !goja.IsNull(v)\n}","tryCatchPattern":"if err := dispatchEvent(p, rt, name, data); err != nil {\n    if strings.Contains(err.Error(), \"siyuan.event is\") {\n        rebuildSandbox(rt)\n    }\n}","preventionTips":["Complete the event-hub injection before any script runs","Audit plugin code for assignments to globalThis.siyuan.event","Re-inject the module if the value becomes undefined/null"],"tags":["goja","plugin-sandbox","events"],"backgroundTag":"null-argument","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}