siyuan-note/siyuan · error
js value cannot be exported to a valid Go value
Error message
js value cannot be exported to a valid Go value
What it means
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.
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
Example fix
// before
await api.request({ data: maybeUndefined });
// after
if (maybeUndefined == null) throw new Error('data is required');
await api.request({ data: maybeUndefined }); Defensive patterns
Strategy: validation
Validate before calling
if (data == null) throw new TypeError('data is required and must not be null/undefined'); Type guard
function isPresent(v) { return v !== undefined && v !== null; } Try / catch
try { await api.send(data); } catch (e) { if (String(e).includes('cannot be exported to a valid Go value')) { data = data ?? ''; } else { throw e; } } Prevention
- Default optional data values (data ?? '') before calling
- Trace undefined origins with strict mode / linters
- Validate payloads before kernel API calls
When it happens
Trigger: Passing undefined, null, or a value whose Export() yields Go nil as the data/body argument of an API expecting bytes.
Common situations: 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.
Related errors
- unsupported data type: %T
- failed to serialize config.
- failed to serialize inputSchema
- globalThis.siyuan.plugin.lifecycle is not an object
- globalThis.siyuan.plugin.lifecycle not found
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/08bddeca568967b1.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/plugin/sandbox.go:489
// jsValueToBytes attempts to convert a goja.Value to a byte slice, supporting string, Buffer, ArrayBuffer, etc.
func jsValueToBytes(rt *goja.Runtime, value goja.Value) (data []byte, err error) {
if goValue := value.Export(); goValue != nil {
switch d := goValue.(type) {
case string: // string
data = []byte(d)
case []byte: // Buffer
data = d
case goja.ArrayBuffer: // ArrayBuffer
data = d.Bytes()
case buffer.Buffer: // ?
data = buffer.Bytes(rt, value)
default:
err = fmt.Errorf("unsupported data type: %T", goValue)
}
return
}
err = fmt.Errorf("js value cannot be exported to a valid Go value")
return
}
// getRequestHandler retrieves the handler function and its containing object for a given scope and request type from the plugin's JS context.
func getRequestHandler(rt *goja.Runtime, scope AccessScope, requestType RequestType) (handler goja.Callable, handlerObj *goja.Object, err error) {
// Get handler object: siyuan.server[scope][requestType]
handlerObjValue, getObjErr := getJsContextValue(rt, []any{"siyuan", "server", string(scope), string(requestType)})
if getObjErr != nil {
err = getObjErr
return
}
handlerObj = handlerObjValue.ToObject(rt)
if handlerObj == nil {
err = fmt.Errorf("globalThis.siyuan.server[%s][%s] is not an object", scope, requestType)
return
}
View on GitHub (pinned to 9f775e8a12)