{"record":{"id":"ab113041bd6551c1","repo":"iflytek/astron-agent","slug":"the-file-address-is-incorrect","errorCode":null,"errorMessage":"The file address is incorrect","messagePattern":"The file address is incorrect","errorType":"error_code","errorClass":"ProtocolParamException","httpStatus":null,"severity":"error","filePath":"core/knowledge/utils/file_utils.py","lineNumber":15,"sourceCode":"import os\nfrom typing import Tuple\nfrom urllib.parse import urlparse\n\nfrom knowledge.exceptions.exception import ProtocolParamException\n\n\ndef get_file_extension_from_url(url: str) -> str:\n    # Use urlparse to parse URL\n    parsed_url = urlparse(url)\n    # Extract path part\n    path = parsed_url.path\n    # If path ends with slash (e.g., directory), there's no file extension\n    if not path or path.endswith(\"/\"):\n        raise ProtocolParamException(\"The file address is incorrect\")\n    # Use os.path.splitext to split filename and extension\n    base_name, extension = os.path.splitext(os.path.basename(path))\n    # Return extension (without dot)\n    return extension[1:] if extension else \"\"\n\n\ndef get_file_info_from_url(url: str) -> Tuple[str, str, str]:\n    # Use urlparse to parse URL\n    parsed_url = urlparse(url)\n    # Extract path part\n    path = parsed_url.path\n    # If path ends with slash (e.g., directory), there's no file extension\n    if not path or path.endswith(\"/\"):\n        raise ProtocolParamException(\"The file address is incorrect\")\n    # Use os.path.splitext to split filename and extension\n    file_name = os.path.basename(path)\n    file_base_name, extension = os.path.splitext(os.path.basename(path))\n    # Return extension (without dot)","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/knowledge/utils/file_utils.py#L1-L33","documentation":"get_file_extension_from_url() raises ProtocolParamException(\"The file address is incorrect\") when the URL's parsed path is empty or ends with a slash, meaning it cannot represent a file with an extension. It relies on urlparse to extract the path component.","triggerScenarios":"Calling get_file_extension_from_url with a URL whose path is empty (e.g. \"https://host\" or just query params) or points to a directory (e.g. \"https://host/dir/\").","commonSituations":"Misconfigured file URLs in upload requests; user-submitted URLs pointing at directory listings; missing path after scheme (e.g. \"https://?a=b\").","solutions":["Validate the URL contains a non-empty file path before calling document_parse","Append a filename to directory URLs or use the actual file URL","Catch ProtocolParamException and return a clear 400 to the client about the bad file address"],"exampleFix":"// before\nurl = \"https://storage.example.com/docs/\"\next = get_file_extension_from_url(url)  # raises\n// after\nurl = \"https://storage.example.com/docs/report.pdf\"\next = get_file_extension_from_url(url)  # \"pdf\"","handlingStrategy":"validation","validationCode":"from urllib.parse import urlparse\n\ndef has_file_path(url: str) -> bool:\n    path = urlparse(url).path\n    return bool(path) and not path.endswith(\"/\")","typeGuard":"def is_file_url(url: object) -> bool:\n    if not isinstance(url, str) or not url:\n        return False\n    path = urlparse(url).path\n    return bool(path) and not path.endswith(\"/\")","tryCatchPattern":"try:\n    ext = get_file_extension_from_url(url)\nexcept ProtocolParamException:\n    return JSONResponse(status_code=400, content={\"message\": \"file address is incorrect\"})","preventionTips":["Validate that URLs point to files, not directories, before parsing","Require a filename in file URL inputs at the API boundary","Unit-test URL helpers with empty-path and trailing-slash cases"],"tags":["python","url","validation"],"backgroundTag":"invalid-url-format","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}