{"record":{"id":"af9c317f9c9304df","repo":"D4Vinci/Scrapling","slug":"filename-must-be-provided","errorCode":null,"errorMessage":"Filename must be provided","messagePattern":"Filename must be provided","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"scrapling/core/shell.py","lineNumber":665,"sourceCode":"                            \"\\n\",\n                            \"\\r\",\n                            \"\\t\",\n                            \" \",\n                        ):\n                            # Remove consecutive white-spaces\n                            txt_content = TextHandler(re_sub(f\"[{s}]+\", s, txt_content))\n                        yield txt_content\n            yield \"\"\n\n    @classmethod\n    def write_content_to_file(\n        cls, page: Selector, filename: str, css_selector: Optional[str] = None, main_content_only: bool = False\n    ) -> None:\n        \"\"\"Write a Selector's content to a file\"\"\"\n        if not page or not isinstance(page, Selector):  # pragma: no cover\n            raise TypeError(\"Input must be of type `Selector`\")\n        elif not filename or not isinstance(filename, str) or not filename.strip():\n            raise ValueError(\"Filename must be provided\")\n        elif not filename.endswith((\".md\", \".html\", \".txt\")):\n            raise ValueError(\"Unknown file type: filename must end with '.md', '.html', or '.txt'\")\n        else:\n            with open(filename, \"w\", encoding=page.encoding) as f:\n                extension = filename.split(\".\")[-1]\n                f.write(\n                    \"\".join(\n                        cls._extract_content(\n                            page,\n                            cls._extension_map[extension],\n                            css_selector=css_selector,\n                            main_content_only=main_content_only,\n                        )\n                    )\n                )\n","sourceCodeStart":647,"sourceCodeEnd":681,"githubUrl":"https://github.com/D4Vinci/Scrapling/blob/5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f/scrapling/core/shell.py#L647-L681","documentation":"write_content_to_file validates that filename is a non-empty, non-whitespace string before use. An empty/None filename, a non-str value, or a whitespace-only string raises ValueError('Filename must be provided'). This fires before the extension check and before the file is opened.","triggerScenarios":"Calling write_content_to_file(page, filename=None) or '' — typically because a path variable from config/argv came through unset, or a f-string built the name from a missing key.","commonSituations":"Optional CLI args or config keys (output file) not provided while the code forwards them anyway; templated filenames where a substitution evaluated to empty.","solutions":["Default the filename explicitly, e.g. filename = filename or 'output.md'","Validate required path config before calling (fail fast at the boundary)","In CLI code, make the output option required=True instead of defaulting to None"],"exampleFix":"# before\nConvertor.write_content_to_file(page, args.output)  # args.output is None\n\n# after\nConvertor.write_content_to_file(page, args.output or 'output.html')","handlingStrategy":"validation","validationCode":"filename = (filename or '').strip() or 'output.html'\nassert isinstance(filename, str) and filename, 'filename required'","typeGuard":"from typing import Any\n\ndef is_valid_filename(value: Any) -> bool:\n    return isinstance(value, str) and bool(value.strip())","tryCatchPattern":null,"preventionTips":["Make output-path options required in CLI wrappers instead of optional-and-None","Default filenames at your boundary before calling library code"],"tags":["shell","validation","file-output","input-validation"],"backgroundTag":null,"analyzedSha":"5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f","analyzedAt":"2026-08-14T22:23:09.440Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}