π OpenHands: AI-Driven Development
fix #1028, /select-file api call on deleted file in Code Editor caused Error (#1158)
Xia Zhenhua8e4c4c994602
diff --git a/frontend/src/services/fileService.ts b/frontend/src/services/fileService.tsindex 1b07ac409d..3e23924c9b 100644--- a/frontend/src/services/fileService.ts+++ b/frontend/src/services/fileService.ts@@ -6,6 +6,9 @@ export type WorkspaceFile = {export async function selectFile(file: string): Promise<string> {const res = await fetch(`/api/select-file?file=${file}`);const data = await res.json();+ if (res.status !== 200) {+ throw new Error(data.error);+ }return data.code as string;}diff --git a/opendevin/server/listen.py b/opendevin/server/listen.pyindex 49881ab453..395c7ca382 100644--- a/opendevin/server/listen.py+++ b/opendevin/server/listen.py@@ -2,7 +2,7 @@ import uuidfrom pathlib import Pathimport litellm-from fastapi import Depends, FastAPI, WebSocket+from fastapi import Depends, FastAPI, WebSocket, Responsefrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.security import HTTPAuthorizationCredentials, HTTPBearerfrom fastapi.staticfiles import StaticFiles@@ -13,6 +13,7 @@ from starlette.responses import JSONResponseimport agenthub # noqa F401 (we import this to get the agents registered)from opendevin import config, filesfrom opendevin.agent import Agent+from opendevin.logger import opendevin_logger as loggerfrom opendevin.server.agent import agent_managerfrom opendevin.server.auth import get_sid_from_token, sign_tokenfrom opendevin.server.session import message_stack, session_manager@@ -126,8 +127,15 @@ def refresh_files():@app.get('/api/select-file')def select_file(file: str):- with open(Path(Path(str(config.get('WORKSPACE_BASE'))), file), 'r') as selected_file:- content = selected_file.read()+ try:+ workspace_base = config.get('WORKSPACE_BASE')+ file_path = Path(workspace_base, file)+ with open(file_path, 'r') as selected_file:+ content = selected_file.read()+ except Exception as e:+ logger.error(f'Error opening file {file}: {e}', exc_info=False)+ error_msg = f'Error opening file: {e}'+ return Response(f'{{"error": "{error_msg}"}}', status_code=500)return {'code': content}