Browse Source

fix #1028, /select-file api call on deleted file in Code Editor caused Error (#1158)

* fix: /select-file on deleted file exception, detail: https://github.com/OpenDevin/OpenDevin/issues/1028

* fix: lint.

* fix: lint.

---------

Co-authored-by: aaren.xzh <aaren.xzh@antfin.com>
Xia Zhenhua 1 year ago
parent
commit
8e4c4c9946
2 changed files with 14 additions and 3 deletions
  1. 3 0
      frontend/src/services/fileService.ts
  2. 11 3
      opendevin/server/listen.py

+ 3 - 0
frontend/src/services/fileService.ts

@@ -6,6 +6,9 @@ export type WorkspaceFile = {
 export async function selectFile(file: string): Promise<string> {
 export async function selectFile(file: string): Promise<string> {
   const res = await fetch(`/api/select-file?file=${file}`);
   const res = await fetch(`/api/select-file?file=${file}`);
   const data = await res.json();
   const data = await res.json();
+  if (res.status !== 200) {
+    throw new Error(data.error);
+  }
   return data.code as string;
   return data.code as string;
 }
 }
 
 

+ 11 - 3
opendevin/server/listen.py

@@ -2,7 +2,7 @@ import uuid
 from pathlib import Path
 from pathlib import Path
 
 
 import litellm
 import litellm
-from fastapi import Depends, FastAPI, WebSocket
+from fastapi import Depends, FastAPI, WebSocket, Response
 from fastapi.middleware.cors import CORSMiddleware
 from fastapi.middleware.cors import CORSMiddleware
 from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
 from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
 from fastapi.staticfiles import StaticFiles
 from fastapi.staticfiles import StaticFiles
@@ -13,6 +13,7 @@ from starlette.responses import JSONResponse
 import agenthub  # noqa F401 (we import this to get the agents registered)
 import agenthub  # noqa F401 (we import this to get the agents registered)
 from opendevin import config, files
 from opendevin import config, files
 from opendevin.agent import Agent
 from opendevin.agent import Agent
+from opendevin.logger import opendevin_logger as logger
 from opendevin.server.agent import agent_manager
 from opendevin.server.agent import agent_manager
 from opendevin.server.auth import get_sid_from_token, sign_token
 from opendevin.server.auth import get_sid_from_token, sign_token
 from opendevin.server.session import message_stack, session_manager
 from opendevin.server.session import message_stack, session_manager
@@ -126,8 +127,15 @@ def refresh_files():
 
 
 @app.get('/api/select-file')
 @app.get('/api/select-file')
 def select_file(file: str):
 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}
     return {'code': content}