{"record":{"id":"1d189a97b55b05c7","repo":"wavetermdev/waveterm","slug":"too-many-return-values-d","errorCode":null,"errorMessage":"too many return values: %d","messagePattern":"too many return values: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/wshutil/wshadapter.go","lineNumber":48,"sourceCode":"\nfunc decodeRtnVals(rtnVals []reflect.Value) (any, error) {\n\tswitch len(rtnVals) {\n\tcase 0:\n\t\treturn nil, nil\n\tcase 1:\n\t\terrIf := rtnVals[0].Interface()\n\t\tif errIf == nil {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, errIf.(error)\n\tcase 2:\n\t\terrIf := rtnVals[1].Interface()\n\t\tif errIf == nil {\n\t\t\treturn rtnVals[0].Interface(), nil\n\t\t}\n\t\treturn rtnVals[0].Interface(), errIf.(error)\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"too many return values: %d\", len(rtnVals))\n\t}\n}\n\nfunc noImplHandler(handler *RpcResponseHandler) bool {\n\thandler.SendResponseError(fmt.Errorf(\"command %q not implemented\", handler.GetCommand()))\n\treturn true\n}\n\nfunc recodeCommandData(command string, data any, commandDataType reflect.Type) (any, error) {\n\tif command == \"\" || commandDataType == nil {\n\t\treturn data, nil\n\t}\n\tmethodDecl := WshCommandDeclMap[command]\n\tif methodDecl == nil {\n\t\treturn data, fmt.Errorf(\"command %q not found\", command)\n\t}\n\tcommandDataPtr := reflect.New(commandDataType).Interface()\n\tif data != nil {","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/wshutil/wshadapter.go#L30-L66","documentation":"decodeRtnVals (pkg/wshutil/wshadapter.go:31) converts reflect.Call results of a wsh command method into (result, error) pairs for the RPC layer, accepting only 0, 1, or 2 return values. If a registered command method returns 3+ values, the adapter cannot map them to the RPC protocol and returns \"too many return values: %d\". This is a programming/declaration error in the command implementation, not a runtime condition.","triggerScenarios":"Defining a WshServer command method (e.g. FooCommand) whose Go signature returns more than (result, error) or (error), then invoking it over wsh RPC; the reflect method lookup in findCmdMethod binds any method matching <cmd>command regardless of return arity.","commonSituations":"Refactoring a command to return extra context/a second value; copying a normal Go helper's multi-return signature into a command method; adding a generic type parameter set that changes the apparent return shape.","solutions":["Change the command method to return at most two values: (resultType, error) or just (error).","Bundle extra return data into a struct and return that struct as the single result.","Review wshrpc.WshCommandDeclMap declarations so the declared return type matches the method signature."],"exampleFix":"// before\nfunc (ws *WshServer) FooCommand(ctx context.Context, data FooData) (string, int, error) { ... }\n// after\ntype FooResult struct { S string; N int }\nfunc (ws *WshServer) FooCommand(ctx context.Context, data FooData) (*FooResult, error) { ... }","handlingStrategy":"validation","validationCode":"// compile-time-ish check that a command method has a legal return arity\nm := reflect.TypeOf((*wshrpc.WshServer)(nil)).MethodByName(\"FooCommand\")\nt := m.Type\nif t.NumOut() > 2 {\n    panic(fmt.Sprintf(\"FooCommand returns %d values; max is (result, error)\", t.NumOut()))\n}","typeGuard":null,"tryCatchPattern":"result, err := wshclient.RpcWshServerCommand(ctx, cmd, data)\nif err != nil && strings.Contains(err.Error(), \"too many return values\") {\n    // command implementation has an illegal signature; fix the method, not the call site\n}","preventionTips":["Follow the command convention strictly: methods must return (T, error), (error), or nothing.","Add a unit test iterating WshCommandDeclMap checking reflect method return arity <= 2.","Never copy ordinary multi-return helper signatures into *Command methods.","Keep wshrpc decl types in sync with implementations after refactors."],"tags":["rpc","reflection","protocol"],"backgroundTag":"invalid-rpc-signature","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}