From 4b4235ad7f3486494246411aba01f14c01cb2d47 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 3 Jun 2026 11:15:38 +0000 Subject: [PATCH] Improve error messages for missing API keys and HTTP errors Show clear guidance when provider has no key configured instead of cryptic 401. Add friendly messages for 429/403 errors during streaming. Co-Authored-By: Claude Opus 4.7 --- zportal-chat.html | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/zportal-chat.html b/zportal-chat.html index f853de8..14f70ac 100644 --- a/zportal-chat.html +++ b/zportal-chat.html @@ -1148,6 +1148,16 @@ function setStreaming(on){ // ── Send Message (SSE Streaming) ── async function sendMessage(text){ if(!text||!activeProvider||streaming) return; + // Check if provider has an API key + if(!activeProvider.api_key){ + hideWelcome(); + history.push({role:'user',content:text}); + addMessage(text,'user',null); + addMessage('No API key configured for '+activeProvider.name+'. Click the key/edit icon next to the provider in the sidebar to add your API key, or select a provider that has one.','assistant',{error:true}); + $('message').value=''; + $('message').style.height='auto'; + return; + } if(!currentSessionId) currentSessionId='sess-'+Date.now(); history.push({role:'user',content:text}); addMessage(text,'user',null); @@ -1178,8 +1188,12 @@ async function sendMessage(text){ const chunk=JSON.parse(line.slice(5)); if(chunk.type==='done'){ streamDone=true; break; } if(chunk.type==='error'){ - if(!bubbleEl) bubbleEl=addMessage(chunk.delta,'assistant',{error:true}); - else bubbleEl.innerHTML+=esc(chunk.delta); + let errMsg=chunk.delta||'Unknown error'; + if(errMsg.includes('HTTP 401')) errMsg='Authentication failed — the API key for this provider is missing or invalid. Open Settings to configure it.'; + else if(errMsg.includes('HTTP 429')) errMsg='Rate limited — too many requests. Wait a moment and try again.'; + else if(errMsg.includes('HTTP 403')) errMsg='Access denied — check your API key and permissions.'; + if(!bubbleEl) bubbleEl=addMessage(errMsg,'assistant',{error:true}); + else bubbleEl.innerHTML+=esc(errMsg); continue; } if(chunk.type==='delta'){