Skip to content

Commit be3a9cd

Browse files
committed
fix: Update error handling in assistant API to provide clearer messages and handle Vercel Security Checkpoint responses
1 parent 21f03bf commit be3a9cd

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

app/api/assistant/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,9 @@ export async function POST(req: NextRequest) {
733733
)
734734
}
735735

736-
if (!process.env.OSM_API_KEY) {
736+
if (!process.env.HACKCLUB_PROXY_API_KEY) {
737737
return NextResponse.json(
738-
{ error: "OSM_API_KEY is not configured on the server." },
738+
{ error: "HACKCLUB_PROXY_API_KEY is not configured on the server." },
739739
{ status: 500 }
740740
)
741741
}

components/qna-chat-interface.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,17 @@ export function QnAChatInterface() {
323323

324324
const contentType = res.headers.get("content-type") ?? ""
325325
if (!res.ok || !contentType.includes("text/event-stream") || !res.body) {
326-
const data = await res.json().catch(() => ({}))
327-
throw new Error((data as { error?: string }).error ?? "Failed to reach assistant")
326+
const raw = await res.text().catch(() => "")
327+
if (raw.includes("Vercel Security Checkpoint")) {
328+
throw new Error("Chat request blocked by Vercel Security Checkpoint. Please try again, or ask the site admin to disable Bot Protection for /api/assistant.")
329+
}
330+
let parsedError = ""
331+
try {
332+
parsedError = (JSON.parse(raw) as { error?: string })?.error ?? ""
333+
} catch {
334+
parsedError = ""
335+
}
336+
throw new Error(parsedError || "Failed to reach assistant")
328337
}
329338

330339
const reader = res.body.getReader()
@@ -402,7 +411,8 @@ export function QnAChatInterface() {
402411
return
403412
}
404413
console.error(err)
405-
setError("Something went wrong while contacting the assistant.")
414+
const errMsg = err instanceof Error ? err.message : "Something went wrong while contacting the assistant."
415+
setError(errMsg)
406416
updateMessageContent(assistantMessageId, (prev) => prev || "Sorry, I couldn't answer that right now.")
407417
} finally {
408418
streamControllerRef.current = null

components/ui/glowing-ai-chat-assistant.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,17 @@ const FloatingAiAssistant: React.FC = () => {
595595

596596
const contentType = res.headers.get("content-type") ?? ""
597597
if (!res.ok || !contentType.includes("text/event-stream") || !res.body) {
598-
const data = await res.json().catch(() => ({}))
599-
throw new Error((data as { error?: string }).error ?? "Failed to reach assistant")
598+
const raw = await res.text().catch(() => "")
599+
if (raw.includes("Vercel Security Checkpoint")) {
600+
throw new Error("Chat request blocked by Vercel Security Checkpoint. Please try again, or ask the site admin to disable Bot Protection for /api/assistant.")
601+
}
602+
let parsedError = ""
603+
try {
604+
parsedError = (JSON.parse(raw) as { error?: string })?.error ?? ""
605+
} catch {
606+
parsedError = ""
607+
}
608+
throw new Error(parsedError || "Failed to reach assistant")
600609
}
601610

602611
const reader = res.body.getReader()
@@ -680,7 +689,8 @@ const FloatingAiAssistant: React.FC = () => {
680689
return
681690
}
682691
console.error(err)
683-
setError("Something went wrong while contacting the assistant.")
692+
const errMsg = err instanceof Error ? err.message : "Something went wrong while contacting the assistant."
693+
setError(errMsg)
684694
updateMessageContent(assistantMessageId, (prev) => prev || "Sorry, I couldn't answer that right now.")
685695
} finally {
686696
streamControllerRef.current = null

0 commit comments

Comments
 (0)