Mahdi Naser Moghadasi commited on
Commit
c8a71e4
·
1 Parent(s): 735ad30

Fix: Return 500 instead of 404 for LLM errors, and fallback to simulation in llmService

Browse files
Files changed (2) hide show
  1. app/api/llm/route.ts +7 -3
  2. lib/llmService.ts +4 -1
app/api/llm/route.ts CHANGED
@@ -113,11 +113,15 @@ export async function POST(request: NextRequest) {
113
  }
114
  }
115
 
116
- // If no response succeeded, use the last one
 
117
  if (!response || !response.ok) {
118
  return NextResponse.json(
119
- { error: lastError || 'All models failed. Please check your API key and try again.' },
120
- { status: response?.status || 500 }
 
 
 
121
  )
122
  }
123
 
 
113
  }
114
  }
115
 
116
+ // If no response succeeded, return error but don't pass through 404
117
+ // Return 500 with error message so client knows route exists
118
  if (!response || !response.ok) {
119
  return NextResponse.json(
120
+ {
121
+ error: lastError || 'All models failed. Please check your API key and try again.',
122
+ content: undefined
123
+ },
124
+ { status: 500 } // Always return 500 for our API errors, not 404
125
  )
126
  }
127
 
lib/llmService.ts CHANGED
@@ -168,7 +168,10 @@ export async function callHuggingFace(
168
  } catch {
169
  errorMessage = `HTTP ${response.status}: ${response.statusText}`
170
  }
171
- throw new Error(errorMessage)
 
 
 
172
  }
173
 
174
  const responseText = await response.text()
 
168
  } catch {
169
  errorMessage = `HTTP ${response.status}: ${response.statusText}`
170
  }
171
+ // If it's a 404 or other error, fall back to simulation instead of throwing
172
+ // This allows the app to continue working
173
+ console.warn('Hugging Face API failed, using simulation:', errorMessage)
174
+ return simulateLLMResponse(messages)
175
  }
176
 
177
  const responseText = await response.text()