siyuan-note/siyuan · error

second argument must be a function

Error message

second argument must be a function

What it means

Thrown by siyuan.rpc.bind when the second argument is not callable. bind needs a function to invoke when the RPC method is called; goja.AssertFunction fails for non-function values.

Source

Thrown at kernel/plugin/api_rpc.go:60

		var name string
		var method goja.Callable
		var descriptions []string
		if len(call.Arguments) < 2 {
			argErr = fmt.Errorf("method name and function required")
		} else {
			nameArg := call.Argument(0)
			methodArg := call.Argument(1)
			descArgs := call.Arguments[2:]
			if goja.IsString(nameArg) {
				name = nameArg.String()
			} else {
				argErr = fmt.Errorf("first argument must be method name string")
			}
			if argErr == nil {
				if methodJs, ok := goja.AssertFunction(methodArg); ok {
					method = methodJs
				} else {
					argErr = fmt.Errorf("second argument must be a function")
				}
			}
			if argErr == nil {
				descriptions = make([]string, len(descArgs))
				for i, a := range descArgs {
					descriptions[i] = a.String()
				}
			}
		}

		runErr := p.worker.Run(func(rt *goja.Runtime) (result any, err error) {
			if argErr != nil {
				err = argErr
				return
			}
			err = p.bindRpcMethod(name, method, descriptions...)
			return
		}, func(rt *goja.Runtime, result any, err error) {

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Pass a function as the second argument: siyuan.rpc.bind('add', (params) => ...).
  2. Ensure the handler reference is not undefined before binding.

Example fix

// before
siyuan.rpc.bind('add', 'adds two numbers');
// after
siyuan.rpc.bind('add', (params) => params.a + params.b, 'adds two numbers');
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof fn !== 'function') throw new TypeError('handler must be a function');
siyuan.rpc.bind(name, fn);

Type guard

const isHandler = (v) => typeof v === 'function';

Prevention

When it happens

Trigger: Calling bind('add', 'description'), bind('add', {}), or bind('add', null).

Common situations: Plugin passes a description string in the handler slot, or the handler reference is undefined when bind runs.

Related errors


AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12). Data as JSON: /api/errors/73e25d476072de8c. Report an issue: GitHub.