From 5a3f8cac02e50cc251f6657bbf9d1f13591ea5ef Mon Sep 17 00:00:00 2001 From: josh <144584931+dancer@users.noreply.github.com> Date: Sun, 21 Sep 2025 12:40:35 +0100 Subject: [PATCH] fix(auth): show gateway dialog for all authentication errors (#1227) --- app/(chat)/api/chat/route.ts | 54 +++++++++++++++++++++++++++++++----- components/chat.tsx | 4 ++- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 7a62f5a..63827cb 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -127,9 +127,23 @@ export async function POST(request: Request) { const chat = await getChatById({ id }); if (!chat) { - const title = await generateTitleFromUserMessage({ - message, - }); + let title = 'New Chat'; + try { + const generatedTitle = await generateTitleFromUserMessage({ + message, + }); + title = generatedTitle; + } catch (titleError) { + if ( + titleError instanceof Error && + (titleError.message?.includes('AI Gateway authentication failed') || + titleError.message?.includes('No authentication provided') || + titleError.message?.includes('AI Gateway requires a valid credit card')) + ) { + throw titleError; + } + console.warn('Title generation failed, using default:', titleError); + } await saveChat({ id, @@ -173,6 +187,22 @@ export async function POST(request: Request) { let finalMergedUsage: AppUsage | undefined; + try { + await myProvider.languageModel(selectedChatModel).doGenerate({ + prompt: [{ role: 'user', content: [{ type: 'text', text: 'test' }] }], + maxOutputTokens: 1, + }); + } catch (testError) { + if ( + testError instanceof Error && + (testError.message?.includes('AI Gateway authentication failed') || + testError.message?.includes('No authentication provided') || + testError.message?.includes('AI Gateway requires a valid credit card')) + ) { + throw testError; + } + } + const stream = createUIMessageStream({ execute: ({ writer: dataStream }) => { const result = streamText({ @@ -263,7 +293,16 @@ export async function POST(request: Request) { } } }, - onError: () => { + onError: (error) => { + // Check for Vercel AI Gateway authentication errors + if ( + error instanceof Error && + (error.message?.includes('AI Gateway authentication failed') || + error.message?.includes('No authentication provided') || + error.message?.includes('AI Gateway requires a valid credit card')) + ) { + throw error; + } return 'Oops, an error occurred!'; }, }); @@ -284,12 +323,13 @@ export async function POST(request: Request) { return error.toResponse(); } - // Check for Vercel AI Gateway credit card error if ( error instanceof Error && - error.message?.includes( + (error.message?.includes( 'AI Gateway requires a valid credit card on file to service requests', - ) + ) || + error.message?.includes('AI Gateway authentication failed') || + error.message?.includes('No authentication provided')) ) { return new ChatSDKError('bad_request:activate_gateway').toResponse(); } diff --git a/components/chat.tsx b/components/chat.tsx index b4998ad..116fc44 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -110,7 +110,9 @@ export function Chat({ if (error instanceof ChatSDKError) { // Check if it's a credit card error if ( - error.message?.includes('AI Gateway requires a valid credit card') + error.message?.includes('AI Gateway requires a valid credit card') || + error.message?.includes('AI Gateway authentication failed') || + error.message?.includes('No authentication provided') ) { setShowCreditCardAlert(true); } else {