{"record":{"id":"9f5a9e9716384892","repo":"provectus/kafka-ui","slug":"unexpected-script-result-s-boolean-should-be-re","errorCode":null,"errorMessage":"Unexpected script result: %s, Boolean should be returned instead","messagePattern":"Unexpected script result: (.+?), Boolean should be returned instead","errorType":"validation","errorClass":"ValidationException","httpStatus":400,"severity":"error","filePath":"kafka-ui-api/src/main/java/com/provectus/kafka/ui/emitter/MessageFilters.java","lineNumber":62,"sourceCode":"    var jsonSlurper = new JsonSlurper();\n    return new Predicate<TopicMessageDTO>() {\n      @SneakyThrows\n      @Override\n      public boolean test(TopicMessageDTO msg) {\n        var bindings = engine.createBindings();\n        bindings.put(\"partition\", msg.getPartition());\n        bindings.put(\"offset\", msg.getOffset());\n        bindings.put(\"timestampMs\", msg.getTimestamp().toInstant().toEpochMilli());\n        bindings.put(\"keyAsText\", msg.getKey());\n        bindings.put(\"valueAsText\", msg.getContent());\n        bindings.put(\"headers\", msg.getHeaders());\n        bindings.put(\"key\", parseToJsonOrReturnAsIs(jsonSlurper, msg.getKey()));\n        bindings.put(\"value\", parseToJsonOrReturnAsIs(jsonSlurper, msg.getContent()));\n        var result = compiledScript.eval(bindings);\n        if (result instanceof Boolean) {\n          return (Boolean) result;\n        } else {\n          throw new ValidationException(\n              \"Unexpected script result: %s, Boolean should be returned instead\".formatted(result));\n        }\n      }\n    };\n  }\n\n  @Nullable\n  private static Object parseToJsonOrReturnAsIs(JsonSlurper parser, @Nullable String str) {\n    if (str == null) {\n      return null;\n    }\n    try {\n      return parser.parseText(str);\n    } catch (Exception e) {\n      return str;\n    }\n  }\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/provectus/kafka-ui/blob/83b5a60cc08501b570a0c4d0b4cdfceb1b88d6b7/kafka-ui-api/src/main/java/com/provectus/kafka/ui/emitter/MessageFilters.java#L44-L80","documentation":"MessageFilters.test evaluates a user-supplied Groovy filter script per message and requires the script to return a Boolean. If the compiled script evaluates to anything else (String, Map, null, Integer), a ValidationException is thrown telling the user to return a Boolean.","triggerScenarios":"Entering a filter in the UI 'Filter messages' dialog whose last expression doesn't evaluate to true/false, e.g. `value != null` returning a String, or a script ending with a statement that returns null.","commonSituations":"Scripts copied from examples that print or return objects; forgetting `return true/false` or a final boolean expression; using `record.value == null` styles that yield null in edge cases; treating it like a predicate language where truthiness coercion happens (Groovy is not used that way here).","solutions":["Make the script's last evaluated expression a boolean, e.g. `return value != null`","Wrap non-boolean logic in a comparison: `record.key == 'x'` instead of `record.key`","Test the script on a single message first; remember null results also fail"],"exampleFix":"// before (script)\nvalue?.name\n// after (script)\nvalue?.name == 'my-event'","handlingStrategy":"validation","validationCode":"function validateFilterScript(src) {\n  const result = evalCompiledPreview(src); // run against a sample message in a sandbox\n  if (typeof result !== 'boolean') {\n    throw new Error('Filter must evaluate to a boolean, got: ' + typeof result);\n  }\n}","typeGuard":"const isBooleanResult = (r) => typeof r === 'boolean';","tryCatchPattern":"try {\n  const filter = MessageFilters.compile(script);\n} catch (ValidationException e) {\n  if (e.getMessage().contains('Boolean should be returned')) {\n    showHint('End your script with a boolean expression, e.g. value != null');\n  }\n}","preventionTips":["End every Groovy filter with an explicit comparison or `return true/false`","Never rely on truthy coercion — Groovy results are checked with instanceof Boolean","Test scripts against a real message before saving the filter"],"tags":["groovy","filter","validation","scripting"],"backgroundTag":"type-mismatch","analyzedSha":"83b5a60cc08501b570a0c4d0b4cdfceb1b88d6b7","analyzedAt":"2026-09-08T04:35:39.002Z","contentChangedAt":"2026-09-08T04:35:39.002Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}