Skip to content

Commit 701bc7c

Browse files
committed
Refactor project generation response handling to ensure content is created while the temp directory is available and improve error handling for file reading.
1 parent ea73a42 commit 701bc7c

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

app/main.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -639,56 +639,40 @@ async def generate_project_sync(request: ProjectRequest):
639639
# Try compiling again
640640
success, output = compiler.build_project(temp_dir)
641641

642+
# Create the response content while the tempdir is still available
643+
all_files_content = ""
644+
for f in file_paths:
645+
try:
646+
file_path = os.path.join(temp_dir, f)
647+
if os.path.exists(file_path):
648+
with open(file_path, 'r') as file:
649+
all_files_content += f"[filename: {f}]\n{file.read()}\n\n"
650+
except Exception as e:
651+
print(f"Error reading file {f}: {e}")
652+
642653
if success:
643-
# Project compiled successfully, try running it
644-
run_success, run_output = compiler.run_project(temp_dir)
645-
654+
# Project compiled successfully
646655
status.update({
647656
"status": "completed",
648657
"message": "Project generated successfully",
649-
"build_output": output,
650-
"run_output": run_output if run_success else "Failed to run project"
658+
"build_output": output
651659
})
652660

653-
# Create the response content while the tempdir is still available
654-
all_files_content = ""
655-
for f in file_paths:
656-
try:
657-
file_path = os.path.join(temp_dir, f)
658-
if os.path.exists(file_path):
659-
with open(file_path, 'r') as file:
660-
all_files_content += f"[filename: {f}]\n{file.read()}\n\n"
661-
except Exception as e:
662-
print(f"Error reading file {f}: {e}")
663-
664661
# Add build status
665662
all_files_content += "\n# Build succeeded\n"
666-
667-
# Return the response while still inside the context
668-
return PlainTextResponse(content=all_files_content)
669663
else:
664+
# Build failed
670665
status.update({
671666
"status": "failed",
672667
"message": "Failed to generate working project",
673668
"build_output": output
674669
})
675670

676-
# Add a return statement here too, don't let execution continue outside the with block
677-
all_files_content = ""
678-
for f in file_paths:
679-
try:
680-
file_path = os.path.join(temp_dir, f)
681-
if os.path.exists(file_path):
682-
with open(file_path, 'r') as file:
683-
all_files_content += f"[filename: {f}]\n{file.read()}\n\n"
684-
except Exception as e:
685-
print(f"Error reading file {f}: {e}")
686-
687671
# Add build status
688672
all_files_content += "\n# Build failed\n"
689-
690-
save_status(temp_dir, status)
691-
return PlainTextResponse(content=all_files_content)
673+
674+
save_status(temp_dir, status)
675+
return PlainTextResponse(content=all_files_content)
692676

693677
except Exception as e:
694678
raise HTTPException(status_code=500, detail=f"Error generating project: {str(e)}")

0 commit comments

Comments
 (0)