Coverage for server / utilities / file_handler.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-04 09:33 +0000

1from pathlib import Path 

2from fastapi import HTTPException 

3import os 

4from server.utilities.error_detail import safe_detail 

5 

6 

7class FileHandler: 

8 @staticmethod 

9 def read_html_file(file_path: str) -> str: 

10 """ 

11 Read HTML file content from the specified path 

12 

13 Args: 

14 file_path (str): Path to the HTML file 

15 

16 Returns: 

17 str: Content of the HTML file 

18 

19 Raises: 

20 HTTPException: If file not found or permission denied 

21 """ 

22 try: 

23 with open(file_path, "r", encoding="utf-8") as file: 

24 return file.read() 

25 except FileNotFoundError: 

26 raise HTTPException(status_code=404, detail=f"File not found: {file_path}") 

27 except PermissionError: 

28 raise HTTPException( 

29 status_code=403, detail=f"Permission denied accessing file: {file_path}" 

30 ) 

31 except Exception as e: 

32 raise HTTPException( 

33 status_code=500, detail=safe_detail(e, "Error reading file") 

34 )