{"record":{"id":"031edb153fdd35f0","repo":"grafana/k6","slug":"method-to-invoke-cannot-be-empty","errorCode":null,"errorMessage":"method to invoke cannot be empty","messagePattern":"method to invoke cannot be empty","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/grpc/client.go","lineNumber":390,"sourceCode":"}\n\n// buildInvokeRequest creates a new InvokeRequest from the given method name, request object and parameters\nfunc (c *Client) buildInvokeRequest(\n\tmethod string,\n\treq sobek.Value,\n\tparams sobek.Value,\n) (grpcext.InvokeRequest, error) {\n\tgrpcReq := grpcext.InvokeRequest{}\n\n\tstate := c.vu.State()\n\tif state == nil {\n\t\treturn grpcReq, common.NewInitContextError(\"invoking RPC methods in the init context is not supported\")\n\t}\n\tif c.conn == nil {\n\t\treturn grpcReq, errors.New(\"no gRPC connection, you must call connect first\")\n\t}\n\tif method == \"\" {\n\t\treturn grpcReq, errors.New(\"method to invoke cannot be empty\")\n\t}\n\tif method[0] != '/' {\n\t\tmethod = \"/\" + method\n\t}\n\tmethodDesc := c.mds[method]\n\tif methodDesc == nil {\n\t\treturn grpcReq, fmt.Errorf(\"method %q not found in file descriptors\", method)\n\t}\n\n\tp, err := newCallParams(c.vu, params)\n\tif err != nil {\n\t\treturn grpcReq, fmt.Errorf(\"invalid GRPC's client.invoke() parameters: %w\", err)\n\t}\n\n\t// k6 GRPC Invoke's default timeout is 2 minutes\n\tif p.Timeout == time.Duration(0) {\n\t\tp.Timeout = 2 * time.Minute\n\t}","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/grpc/client.go#L372-L408","documentation":"buildInvokeRequest validates the method string right after the connection check: an empty method name cannot identify any RPC, so it is rejected before the method-descriptor lookup. (A non-empty but unknown method gets a different error: 'method %q not found in file descriptors'.)","triggerScenarios":"client.invoke('', req) with a literal empty string, or a computed method name that is empty because of a typo/failed lookup (e.g. methods[i] where i is out of range, or a config field that is missing).","commonSituations":"Building the method name from config or a loop over an empty array; refactoring that renames a constant to undefined; passing the request payload and method in swapped order so the first argument is an object coerced oddly; strings with only whitespace still count as non-empty and fail later at the descriptor lookup.","solutions":["Pass the fully-qualified method as 'package.Service/Method' (leading '/' is added automatically if missing).","When method names come from data, validate them first: if (!method) throw new Error('method name required').","If you get 'method not found in file descriptors' instead, the name is fine but the proto was not loaded - check client.load()."],"exampleFix":"// before\nclient.invoke('', { name: 'k6' }); // throws\n\n// after\nclient.invoke('hello.HelloService/SayHello', { name: 'k6' });","handlingStrategy":"type-guard","validationCode":"function invokeGrpc(client, method, req) {\n  if (typeof method !== 'string' || method.trim() === '') {\n    throw new TypeError(`gRPC method must be a non-empty 'package.Service/Method' string`);\n  }\n  return client.invoke(method, req);\n}","typeGuard":"const isGrpcMethod = (m) => typeof m === 'string' && m.trim().length > 0;","tryCatchPattern":"try {\n  client.invoke(method, req);\n} catch (e) {\n  if (/cannot be empty/.test(e.message)) {\n    throw new Error(`method name was empty - check the config source: ${JSON.stringify(methodCfg)}`);\n  }\n  throw e;\n}","preventionTips":["Centralize method names in a constants object so typos surface at review.","When methods come from data/config, validate them in init before the test starts."],"tags":["k6","grpc","invoke","validation","method-name"],"backgroundTag":"invalid-argument-value","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}