{"record":{"id":"bf89d170c8351b7c","repo":"apache/flink","slug":"a-user-provided-generator-function-threw-an-except-bf89d1","errorCode":null,"errorMessage":"A user-provided generator function threw an exception on this input: %s","messagePattern":"A user-provided generator function threw an exception on this input: (.+?)","errorType":"exception","errorClass":"FlinkRuntimeException","httpStatus":null,"severity":"error","filePath":"flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/source/GeneratingIteratorSourceReader.java","lineNumber":60,"sourceCode":"\n    public GeneratingIteratorSourceReader(\n            SourceReaderContext context, GeneratorFunction<E, O> generatorFunction) {\n        super(context);\n        this.generatorFunction = checkNotNull(generatorFunction);\n    }\n\n    // ------------------------------------------------------------------------\n\n    @Override\n    protected O convert(E value) {\n        try {\n            return generatorFunction.map(value);\n        } catch (Exception e) {\n            String message =\n                    String.format(\n                            \"A user-provided generator function threw an exception on this input: %s\",\n                            value.toString());\n            throw new FlinkRuntimeException(message, e);\n        }\n    }\n\n    @Override\n    public void start(SourceReaderContext context) {\n        try {\n            generatorFunction.open(context);\n        } catch (Exception e) {\n            throw new FlinkRuntimeException(\"Failed to open the GeneratorFunction\", e);\n        }\n    }\n\n    @Override\n    public void close() throws Exception {\n        generatorFunction.close();\n        super.close();\n    }\n}","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/source/GeneratingIteratorSourceReader.java#L42-L78","documentation":"Thrown by convert() in GeneratingIteratorSourceReader when generatorFunction.map(value) throws any exception. The message uses String.format to include the toString() of the specific input value that caused the failure, and the original exception is wrapped in FlinkRuntimeException. This is the production-oriented iterator source reader.","triggerScenarios":"The user-provided GeneratorFunction's map() method throws on a specific input value from the iterator split. Causes include NPE, type cast failures, business logic exceptions, or unhandled edge cases.","commonSituations":"Generator function that does not handle null or unexpected fields; lookup function index out of bounds; arithmetic error on specific input; type mismatch between the iterator element type and what map() expects.","solutions":["Inspect the input value shown in the error message to identify the problematic data.","Add input validation and null-checks in the GeneratorFunction.map() method.","Add try-catch in map() for known edge cases, returning a default or using a side output.","Fix the root cause in the mapping logic for the failing input."],"exampleFix":"// before: map() throws on null input\nGeneratorFunction<Integer, String> fn = value -> value.toString().substring(2);\n\n// after: validate input before processing\nGeneratorFunction<Integer, String> fn = value -> {\n    if (value == null || value < 100) return \"N/A\";\n    return value.toString().substring(2);\n};","handlingStrategy":"try-catch","validationCode":"// Validate map() handles all known iterator inputs\nGeneratorFunction<E, O> fn = ...;\nfor (E testInput : sampleInputs) {\n    fn.map(testInput); // should not throw for valid inputs\n}","typeGuard":null,"tryCatchPattern":"try {\n    sourceReader.pollNext(output);\n} catch (FlinkRuntimeException e) {\n    if (e.getMessage().startsWith(\"A user-provided generator function threw\")) {\n        // extract the failing input value from the message\n        // add validation in map() for this input type\n    }\n    throw e;\n}","preventionTips":["Add input validation and null-checks in GeneratorFunction.map().","Unit-test map() with representative and edge-case inputs.","Use try-catch inside map() for non-critical edge cases."],"tags":["datagen","generator-function","user-code","runtime"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}