{"record":{"id":"08bddeca568967b1","repo":"siyuan-note/siyuan","slug":"js-value-cannot-be-exported-to-a-valid-go-value","errorCode":null,"errorMessage":"js value cannot be exported to a valid Go value","messagePattern":"js value cannot be exported to a valid Go value","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/plugin/sandbox.go","lineNumber":489,"sourceCode":"\n// jsValueToBytes attempts to convert a goja.Value to a byte slice, supporting string, Buffer, ArrayBuffer, etc.\nfunc jsValueToBytes(rt *goja.Runtime, value goja.Value) (data []byte, err error) {\n\tif goValue := value.Export(); goValue != nil {\n\t\tswitch d := goValue.(type) {\n\t\tcase string: // string\n\t\t\tdata = []byte(d)\n\t\tcase []byte: // Buffer\n\t\t\tdata = d\n\t\tcase goja.ArrayBuffer: // ArrayBuffer\n\t\t\tdata = d.Bytes()\n\t\tcase buffer.Buffer: // ?\n\t\t\tdata = buffer.Bytes(rt, value)\n\t\tdefault:\n\t\t\terr = fmt.Errorf(\"unsupported data type: %T\", goValue)\n\t\t}\n\t\treturn\n\t}\n\terr = fmt.Errorf(\"js value cannot be exported to a valid Go value\")\n\treturn\n}\n\n// getRequestHandler retrieves the handler function and its containing object for a given scope and request type from the plugin's JS context.\nfunc getRequestHandler(rt *goja.Runtime, scope AccessScope, requestType RequestType) (handler goja.Callable, handlerObj *goja.Object, err error) {\n\t// Get handler object: siyuan.server[scope][requestType]\n\thandlerObjValue, getObjErr := getJsContextValue(rt, []any{\"siyuan\", \"server\", string(scope), string(requestType)})\n\tif getObjErr != nil {\n\t\terr = getObjErr\n\t\treturn\n\t}\n\n\thandlerObj = handlerObjValue.ToObject(rt)\n\tif handlerObj == nil {\n\t\terr = fmt.Errorf(\"globalThis.siyuan.server[%s][%s] is not an object\", scope, requestType)\n\t\treturn\n\t}\n","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/plugin/sandbox.go#L471-L507","documentation":"jsValueToBytes first exports the JS value to Go; if Export() returns nil (the value is nil/undefined or exports to a nil Go value), it cannot be converted at all and this generic error is returned instead of bytes.","triggerScenarios":"Passing undefined, null, or a value whose Export() yields Go nil as the data/body argument of an API expecting bytes.","commonSituations":"Omitting a required data parameter, a variable that is undefined due to a plugin bug, or passing the result of a failed lookup (null) straight into the API.","solutions":["Ensure the data argument is defined before calling: default it (data ?? '')","Check upstream plugin logic that produced undefined/null","Validate the payload shape prior to the API call"],"exampleFix":"// before\nawait api.request({ data: maybeUndefined });\n// after\nif (maybeUndefined == null) throw new Error('data is required');\nawait api.request({ data: maybeUndefined });","handlingStrategy":"validation","validationCode":"if (data == null) throw new TypeError('data is required and must not be null/undefined');","typeGuard":"function isPresent(v) { return v !== undefined && v !== null; }","tryCatchPattern":"try { await api.send(data); } catch (e) { if (String(e).includes('cannot be exported to a valid Go value')) { data = data ?? ''; } else { throw e; } }","preventionTips":["Default optional data values (data ?? '') before calling","Trace undefined origins with strict mode / linters","Validate payloads before kernel API calls"],"tags":["goja","plugin","null-value","serialization"],"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"}