{"record":{"id":"e011af41467fa30f","repo":"apache/pulsar","slug":"function-may-not-return-more-than-two-values","errorCode":null,"errorMessage":"function may not return more than two values","messagePattern":"function may not return more than two values","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pulsar-function-go/pf/function.go","lineNumber":83,"sourceCode":"\t\treturn false, fmt.Errorf(\"functions may not take more than two arguments, but function takes %d\", handler.NumIn())\n\t} else if handler.NumIn() > 0 {\n\t\tcontextType := reflect.TypeOf((*context.Context)(nil)).Elem()\n\t\targumentType := handler.In(0)\n\t\thandlerTakesContext = argumentType.Implements(contextType)\n\t\tif handler.NumIn() > 1 && !handlerTakesContext {\n\t\t\treturn false, fmt.Errorf(\"function takes two arguments, but the first is not Context. got %s\", argumentType.Kind())\n\t\t}\n\t}\n\n\treturn handlerTakesContext, nil\n}\n\nfunc validateReturns(handler reflect.Type) error {\n\terrorType := reflect.TypeOf((*error)(nil)).Elem()\n\n\tswitch {\n\tcase handler.NumOut() > 2:\n\t\treturn fmt.Errorf(\"function may not return more than two values\")\n\tcase handler.NumOut() > 1:\n\t\tif !handler.Out(1).Implements(errorType) {\n\t\t\treturn fmt.Errorf(\"function returns two values, but the second does not implement error\")\n\t\t}\n\tcase handler.NumOut() == 1:\n\t\tif !handler.Out(0).Implements(errorType) {\n\t\t\treturn fmt.Errorf(\"function returns a single value, but it does not implement error\")\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc newFunction(inputFunc interface{}) function {\n\tif inputFunc == nil {\n\t\treturn errorHandler(fmt.Errorf(\"function is nil\"))\n\t}\n\thandler := reflect.ValueOf(inputFunc)","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-function-go/pf/function.go#L65-L101","documentation":"validateReturns restricts handler return arity: a Go function may return either nothing, one error, or (result, error). A handler whose reflect type has NumOut() > 2 cannot be invoked correctly by the framework, so newFunction fails with this error.","triggerScenarios":"Registering a handler returning three or more values, e.g. func(ctx context.Context, msg []byte) ([]byte, []byte, error), or returning multiple results plus error.","commonSituations":"Trying to return both an output message and extra diagnostics; porting handlers that return (value, metadata, error); misunderstanding that only a single output plus error is supported.","solutions":["Return at most two values: a single output of type T plus an error.","Pack extra outputs into the single result (struct, map, or serialized payload).","Log diagnostics instead of returning them; publish additional data to other topics via the output topic.","Adapt with a wrapper that drops/merges extra return values into one."],"exampleFix":"// before\nfunc handle(ctx context.Context, msg []byte) ([]byte, []byte, error) { ... }\n// after\ntype result struct{ Out []byte; Extra []byte }\nfunc handle(ctx context.Context, msg []byte) (result, error) { ... }","handlingStrategy":"validation","validationCode":"func validReturnArity(handler interface{}) bool {\n    t := reflect.TypeOf(handler)\n    return t != nil && t.Kind() == reflect.Func && t.NumOut() <= 2\n}","typeGuard":"func returnsAtMostTwo(handler interface{}) bool {\n    t := reflect.TypeOf(handler)\n    return t.Kind() == reflect.Func && t.NumOut() <= 2\n}","tryCatchPattern":"f, err := pf.NewGoFunction(ctx, pf.GoFunction{\n    Handler: myHandler,\n})\nif err != nil {\n    log.Fatalf(\"invalid handler returns: %v\", err)\n}","preventionTips":["Follow the (T, error) convention — one output plus error, nothing more.","Aggregate multiple outputs into a single struct or serialized payload.","Publish side outputs to separate topics instead of returning them.","Review signatures after refactors that add return values."],"tags":["go","pulsar-functions","reflection","return-values"],"backgroundTag":"invalid-function-signature","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}