{"record":{"id":"7f07c0f5550e4ba0","repo":"apache/pulsar","slug":"function-returns-a-single-value-but-it-does-not-i","errorCode":null,"errorMessage":"function returns a single value, but it does not implement error","messagePattern":"function returns a single value, but it does not implement error","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pulsar-function-go/pf/function.go","lineNumber":90,"sourceCode":"\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)\n\thandlerType := reflect.TypeOf(inputFunc)\n\tif handlerType.Kind() != reflect.Func {\n\t\treturn errorHandler(fmt.Errorf(\"function kind %s is not %s\", handlerType.Kind(), reflect.Func))\n\t}\n\n\ttakesContext, err := validateArguments(handlerType)\n\tif err != nil {","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-function-go/pf/function.go#L72-L108","documentation":"A single-value handler must return only an error (func(msg T) error), since with one output there is nowhere to put a result. Reflection checks handler.Out(0).Implements(errorType); returning any other single value fails registration in newFunction.","triggerScenarios":"Registering func(msg []byte) []byte or func(ctx context.Context, msg []byte) string — NumOut()==1 but the sole return type does not implement error.","commonSituations":"Writing map-style handlers that return only the transformed value, assuming return-only-value signatures are supported; forgetting to return an error alongside the result; porting from frameworks that accept bare-value handlers.","solutions":["Return (T, error) instead of just T so the value is recognized as output.","If the function has no meaningful output, return only error: func(msg T) error.","Never return a bare non-error value; the framework treats lone error as a success-with-no-output contract.","Adapt existing logic with a wrapper returning (result, nil)."],"exampleFix":"// before\nfunc handle(ctx context.Context, msg []byte) []byte { ... }\n// after\nfunc handle(ctx context.Context, msg []byte) ([]byte, error) {\n    return transform(msg), nil\n}","handlingStrategy":"validation","validationCode":"func singleReturnIsError(handler interface{}) bool {\n    t := reflect.TypeOf(handler)\n    errType := reflect.TypeOf((*error)(nil)).Elem()\n    return t.Kind() == reflect.Func && t.NumOut() == 1 && t.Out(0).Implements(errType)\n}","typeGuard":"func isErrOnlyHandler(handler interface{}) bool {\n    t := reflect.TypeOf(handler)\n    errType := reflect.TypeOf((*error)(nil)).Elem()\n    return t.Kind() == reflect.Func && t.NumOut() == 1 && t.Out(0).Implements(errType)\n}","tryCatchPattern":"f, err := pf.NewGoFunction(ctx, pf.GoFunction{\n    Handler: myHandler,\n})\nif err != nil {\n    if strings.Contains(err.Error(), \"single value\") {\n        log.Fatalf(\"single-return handler must return only error: %v\", err)\n    }\n    log.Fatalf(\"function registration failed: %v\", err)\n}","preventionTips":["If the handler produces output, always return (T, error) — never a bare T.","Reserve the one-return form strictly for error-only (sink-like) functions.","Add a compile-adjacent unit test that instantiates the function object.","Document each handler's signature convention in your function package README."],"tags":["go","pulsar-functions","reflection","error-handling"],"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-14T05:17:10.506Z"}