fix(auth): show gateway dialog for all authentication errors (#1227)
This commit is contained in:
parent
1aff7d9868
commit
5a3f8cac02
2 changed files with 50 additions and 8 deletions
|
|
@ -127,9 +127,23 @@ export async function POST(request: Request) {
|
||||||
const chat = await getChatById({ id });
|
const chat = await getChatById({ id });
|
||||||
|
|
||||||
if (!chat) {
|
if (!chat) {
|
||||||
const title = await generateTitleFromUserMessage({
|
let title = 'New Chat';
|
||||||
|
try {
|
||||||
|
const generatedTitle = await generateTitleFromUserMessage({
|
||||||
message,
|
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({
|
await saveChat({
|
||||||
id,
|
id,
|
||||||
|
|
@ -173,6 +187,22 @@ export async function POST(request: Request) {
|
||||||
|
|
||||||
let finalMergedUsage: AppUsage | undefined;
|
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({
|
const stream = createUIMessageStream({
|
||||||
execute: ({ writer: dataStream }) => {
|
execute: ({ writer: dataStream }) => {
|
||||||
const result = streamText({
|
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!';
|
return 'Oops, an error occurred!';
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
@ -284,12 +323,13 @@ export async function POST(request: Request) {
|
||||||
return error.toResponse();
|
return error.toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for Vercel AI Gateway credit card error
|
|
||||||
if (
|
if (
|
||||||
error instanceof Error &&
|
error instanceof Error &&
|
||||||
error.message?.includes(
|
(error.message?.includes(
|
||||||
'AI Gateway requires a valid credit card on file to service requests',
|
'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();
|
return new ChatSDKError('bad_request:activate_gateway').toResponse();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,9 @@ export function Chat({
|
||||||
if (error instanceof ChatSDKError) {
|
if (error instanceof ChatSDKError) {
|
||||||
// Check if it's a credit card error
|
// Check if it's a credit card error
|
||||||
if (
|
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);
|
setShowCreditCardAlert(true);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue