{"record":{"id":"4f06e7b6eaa663e2","repo":"oraios/serena","slug":"invalid-mode-self-mode-expected-literal-or","errorCode":null,"errorMessage":"Invalid mode: '{self.mode}', expected 'literal' or 'regex'.","messagePattern":"Invalid mode: '(.+?)', expected 'literal' or 'regex'\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/serena/util/text_utils.py","lineNumber":463,"sourceCode":"        repl: str,\n    ) -> str:\n        \"\"\"\n        Performs the replacement.\n\n        Raises ValueError if no match is found, or if multiple matches are found while allow_multiple_occurrences is False.\n\n        :param content: the content in which to perform the replacement\n        :param needle: the search expression, which is either a literal string or a regular expression, depending on the mode\n        :param repl: the replacement string, which, in regex mode, may contain backreferences in the form of $!1, $!2, etc. to\n            refer to matched groups in the search expression\n        :return: the updated content after performing the replacement\n        \"\"\"\n        if self.mode == \"literal\":\n            regex = re.escape(needle)\n        elif self.mode == \"regex\":\n            regex = needle\n        else:\n            raise ValueError(f\"Invalid mode: '{self.mode}', expected 'literal' or 'regex'.\")\n\n        regex_flags = (re.MULTILINE | re.DOTALL) if self.regex_multiline else 0\n\n        # create replacement function with validation and backreference handling\n        repl_fn = self._create_replacement_function(regex, repl, regex_flags=regex_flags)\n\n        # perform replacement\n        updated_content, n = re.subn(regex, repl_fn, content, flags=regex_flags)\n\n        if n == 0:\n            raise ValueError(\"Error: No matches of search expression found.\")\n        if not self.allow_multiple_occurrences and n > 1:\n            raise ValueError(\n                f\"Expression matches {n} occurrences. \"\n                \"Please revise the expression to be more specific or enable allow_multiple_occurrences if this is expected.\"\n            )\n        return updated_content\n","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/util/text_utils.py#L445-L481","documentation":"Thrown by the replace() method (and RegExpEditMode construction) when the mode attribute is neither 'literal' nor 'regex'. Serena only supports these two modes for how the needle is interpreted, and it validates strictly rather than guessing.","triggerScenarios":"Passing a mode string with different casing ('Literal'), a typo ('regex ' with trailing space, 'regexpr'), or a None/other value in an object using this text-editing utility.","commonSituations":"Config files or templates where the mode is read from user input or YAML/JSON and not validated; refactors that renamed mode constants.","solutions":["Set mode to exactly 'literal' or 'regex' (lowercase)","Strip and normalize any user-supplied mode string before constructing the edit","Add a pre-construction validation that rejects unknown modes with a clearer message"],"exampleFix":"// before\nTextReplacement(mode='Literal', needle='foo', repl='bar')\n// after\nmode = user_mode.strip().lower()\nassert mode in ('literal', 'regex')\nTextReplacement(mode=mode, needle='foo', repl='bar')","handlingStrategy":"validation","validationCode":"if mode not in ('literal', 'regex'):\n    raise ValueError(f'mode must be literal or regex, got {mode!r}')","typeGuard":"def is_valid_mode(mode: object) -> TypeGuard[Literal['literal','regex']]:\n    return mode in ('literal', 'regex')","tryCatchPattern":"try:\n    editor.replace(content, needle, repl)\nexcept ValueError as e:\n    if 'Invalid mode' in str(e):\n        editor.mode = 'literal'\n        updated = editor.replace(content, needle, repl)\n    else:\n        raise","preventionTips":["Use Literal['literal','regex'] type annotations","Normalize user/config-supplied mode with .strip().lower()","Centralize mode selection in one enum-like constant set"],"tags":["regex","validation","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}