|
9 | 9 | import os |
10 | 10 | from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped |
11 | 11 | from sqlalchemy import create_engine |
| 12 | +import sqlalchemy.exc |
12 | 13 | from sqlalchemy.orm import Session |
13 | 14 | from typing import Optional |
14 | 15 | from pathlib import Path |
@@ -53,7 +54,11 @@ def __repr__(self): |
53 | 54 | SEARCH_RESULT_DIR = mcp_data_dir("seclab-taskflows", "gh_file_viewer", "SEARCH_RESULTS_DIR") |
54 | 55 |
|
55 | 56 | engine = create_engine(f"sqlite:///{os.path.abspath(SEARCH_RESULT_DIR)}/search_result.db", echo=False) |
56 | | -Base.metadata.create_all(engine, tables=[SearchResults.__table__]) |
| 57 | + |
| 58 | +try: |
| 59 | + Base.metadata.create_all(engine, tables=[SearchResults.__table__]) |
| 60 | +except sqlalchemy.exc.OperationalError as e: |
| 61 | + logging.exception(f"Database/Tables already exist(s)") # only log here, as this error likely only happens in test |
57 | 62 |
|
58 | 63 |
|
59 | 64 | async def call_api(url: str, params: dict) -> str: |
@@ -283,10 +288,13 @@ async def list_directory_from_gh( |
283 | 288 | r = await call_api(url=f"https://api.github.com/repos/{owner}/{repo}/contents/{path}", params={}) |
284 | 289 | if isinstance(r, str): |
285 | 290 | return r |
286 | | - if not r.json(): |
| 291 | + data = r.json() |
| 292 | + if not data: |
287 | 293 | return json.dumps([], indent=2) |
| 294 | + if not isinstance(data, list): |
| 295 | + return f"Path '{path}' is not a directory." |
288 | 296 |
|
289 | | - content = [item["path"] for item in r.json()] |
| 297 | + content = [item["path"] for item in data] |
290 | 298 | return json.dumps(content, indent=2) |
291 | 299 |
|
292 | 300 |
|
|
0 commit comments