{"record":{"id":"96401cde50435be3","repo":"ZhuLinsen/daily_stock_analysis","slug":"invalid-import-file","errorCode":"invalid_import_file","errorMessage":"invalid_import_file","messagePattern":"invalid_import_file","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"warning","filePath":"api/v1/endpoints/system_config.py","lineNumber":501,"sourceCode":"    request_obj: Request,\n    service: SystemConfigService = Depends(get_system_config_service),\n) -> UpdateSystemConfigResponse:\n    \"\"\"Import a `.env` backup into the active config.\"\"\"\n    try:\n        _allow_env_backup_access(request_obj)\n    except EnvBackupAccessDenied as exc:\n        logger.warning(\"System config import blocked: %s\", exc)\n        _raise_env_backup_access_error(exc)\n\n    try:\n        payload = service.import_env(\n            config_version=request.config_version,\n            content=request.content,\n            reload_now=request.reload_now,\n        )\n        return UpdateSystemConfigResponse.model_validate(payload)\n    except ConfigImportError as exc:\n        raise HTTPException(\n            status_code=400,\n            detail={\n                \"error\": \"invalid_import_file\",\n                \"message\": exc.message,\n            },\n        )\n    except ConfigValidationError as exc:\n        raise HTTPException(\n            status_code=400,\n            detail={\n                \"error\": \"validation_failed\",\n                \"message\": \"System configuration validation failed\",\n                \"issues\": exc.issues,\n            },\n        )\n    except ConfigConflictError as exc:\n        raise HTTPException(\n            status_code=409,","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/api/v1/endpoints/system_config.py#L483-L519","documentation":"400 with error code 'invalid_import_file' raised when service.import_env() raises ConfigImportError, meaning the submitted backup content is not a parseable .env file (structure problem), as opposed to semantic validation failures which raise ConfigValidationError. The detail message comes from exc.message, which states exactly what could not be parsed.","triggerScenarios":"POST /api/v1/system/config/config/import with request.content that is malformed .env syntax: missing '=' in a line, invalid quoting, BOM/UTF-16 encoding from a Windows editor, truncated file, or JSON accidentally pasted instead of .env text.","commonSituations":"Importing a backup edited by hand; uploading the wrong file (report JSON, YAML config) to the import dialog; backups saved with Excel or Notepad that added a BOM or CRLF-only corruption; partial file transfer.","solutions":["Read the 'message' field in the 400 detail — it names the specific parse problem and usually the offending line.","Validate the file locally: every non-comment line must be KEY=VALUE with balanced quotes; strip any BOM.","Re-export a fresh backup from the source instance and import that unmodified file to confirm the pipeline works.","If editing is required, use a plain-text editor and keep UTF-8 without BOM."],"exampleFix":"# before (broken): line without '='\nOPENAI_API_KEY sk-xxxx\n\n# after\nOPENAI_API_KEY=sk-xxxx","handlingStrategy":"validation","validationCode":"def looks_like_env(content: str) -> bool:\n    if content.startswith('\\ufeff'):\n        return False  # BOM\n    for i, line in enumerate(content.splitlines(), 1):\n        s = line.strip()\n        if not s or s.startswith('#'):\n            continue\n        if '=' not in s:\n            return False\n    return True\n\nassert looks_like_env(request_content), 'not a parseable .env payload'","typeGuard":null,"tryCatchPattern":"resp = client.post('/config/import', json=body)\nif resp.status_code == 400 and resp.json()['detail']['error'] == 'invalid_import_file':\n    show(resp.json()['detail']['message'])  # names the offending line; fix content, don't retry blind","preventionTips":["Import only files produced by /config/export, unmodified.","If editing is needed, keep UTF-8 without BOM and KEY=VALUE on every active line.","Run a local dotenv parser (python-dotenv) over the content before uploading."],"tags":["fastapi","http-400","import","dotenv","validation"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}