Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| const response = await fetch(`/api/api-keys/check?agent=${selectedAgent}&model=${selectedModel}`) | ||
| const data = await response.json() | ||
|
|
||
| if (!data.hasKey) { | ||
| // Show error message with provider name | ||
| const providerNames: Record<string, string> = { | ||
| anthropic: 'Anthropic', | ||
| openai: 'OpenAI', | ||
| cursor: 'Cursor', | ||
| gemini: 'Gemini', | ||
| aigateway: 'AI Gateway', | ||
| } | ||
| const providerName = providerNames[data.provider] || data.provider | ||
|
|
||
| toast.error(`${providerName} API key required`, { | ||
| description: `Please add your ${providerName} API key to use the ${data.agentName} agent with this model.`, | ||
| action: { | ||
| label: 'Add API Key', | ||
| onClick: () => setShowApiKeysDialog(true), | ||
| }, | ||
| }) | ||
| return | ||
| } |
There was a problem hiding this comment.
Missing response status check before accessing API response data, causing error responses to show "undefined API key required" instead of proper error messages.
View Details
📝 Patch Details
diff --git a/components/task-form.tsx b/components/task-form.tsx
index dfe5c76..bab118f 100644
--- a/components/task-form.tsx
+++ b/components/task-form.tsx
@@ -271,6 +271,15 @@ export function TaskForm({
// Check if API key is required and available for the selected agent and model
try {
const response = await fetch(`/api/api-keys/check?agent=${selectedAgent}&model=${selectedModel}`)
+
+ if (!response.ok) {
+ const error = await response.json()
+ toast.error('Failed to validate API key availability', {
+ description: error.error || 'An error occurred while checking API key availability',
+ })
+ return
+ }
+
const data = await response.json()
if (!data.hasKey) {
Analysis
Missing response status check in API key validation causes misleading error messages
What fails: components/task-form.tsx handleSubmit() calls response.json() without checking response.ok, causing error responses to be processed as valid data
How to reproduce:
# API returns error: { error: 'Invalid agent' } with status 400
# Current code accesses data.hasKey, data.provider, data.agentName without checking response.okResult: When API returns { error: 'Invalid agent' } (status 400), data.hasKey is undefined, so the code shows toast: "undefined API key required" with description "Please add your undefined API key to use the undefined agent with this model."
Expected: Should check response.ok first (following Fetch API pattern used in 21 other locations in codebase including line 251 of same file), show proper error message from API response
No description provided.