Skip to content

Commit 46790a9

Browse files
committed
fix: robust error handling and api key flow
1 parent a364d8b commit 46790a9

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎api/routes/query.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,15 @@ async def query_endpoint(
4545
Write operations require explicit approval via /api/approve.
4646
"""
4747
try:
48+
import os
49+
if not x_openai_key and not os.getenv("OPENAI_API_KEY"):
50+
raise ValueError("OpenAI API Key is missing. Please add it in the Settings panel.")
51+
4852
result = await run_query(request.question, api_key=x_openai_key)
4953
return QueryResponse(**result)
54+
except ValueError as ve:
55+
logger.warning("Validation error: %s", ve)
56+
raise HTTPException(status_code=400, detail=str(ve))
5057
except Exception as exc:
5158
logger.error("Query endpoint error: %s", exc, exc_info=True)
5259
raise HTTPException(status_code=500, detail=str(exc))

‎api/routes/upload.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def upload_database(
3232
shutil.copyfileobj(file.file, buffer)
3333

3434
# Update database engine
35-
set_engine_url(f"sqlite:///{file_path}")
35+
set_engine_url("sqlite:///./data/uploaded.db")
3636

3737
# Connect and extract tables
3838
conn = sqlite3.connect(file_path)

‎frontend/src/components/ChatWindow.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export default function ChatWindow({ messages, setMessages, onAnswer, injectedQu
4848
onAnswer(response)
4949
} catch (err: any) {
5050
let errMsg = err instanceof Error ? err.message : String(err)
51+
52+
// Extract backend detail if available
53+
if (err.response?.data?.detail) {
54+
errMsg = err.response.data.detail
55+
}
56+
5157
if (errMsg.includes('405') || errMsg.includes('404') || errMsg.includes('Network Error')) {
5258
errMsg = "Failed to connect to the backend. If you are on GitHub Pages, click the ⚙️ Settings icon in the top right to configure your local Backend API URL (e.g., http://localhost:8000)."
5359
}

‎frontend/src/components/Navbar.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ export default function Navbar() {
4545
setUploadProgress(0)
4646

4747
try {
48-
// Need to set API URL first if they changed it, so upload goes to the right place
48+
// Need to save settings first so the interceptor picks up the latest API key and URL
4949
setApiUrl(apiUrlInput)
50+
setApiKey(apiKeyInput)
51+
5052
const res = await uploadDatabase(file, (pct) => setUploadProgress(pct))
5153
if (res.success) {
5254
setUploadSuccess(res.message || 'Database uploaded and indexed successfully!')

0 commit comments

Comments
 (0)