From c9073bb4bd6add1294ac0a78b3c0ecb5d15b4acb Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 23 Sep 2025 14:34:50 +0700 Subject: [PATCH 1/3] wip: when creating a project fails, check for auth errors with the api call to feed back to the user what/why we had an issue --- src/components/ui/toast.tsx | 4 +-- src/lib/i18n.ts | 20 +++++++++++++ src/pages/Index.tsx | 56 +++++++++++++++++++++++++++++++++---- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/components/ui/toast.tsx b/src/components/ui/toast.tsx index a8224775..38fdfa25 100644 --- a/src/components/ui/toast.tsx +++ b/src/components/ui/toast.tsx @@ -14,7 +14,7 @@ const ToastViewport = React.forwardRef< , React.ComponentPropsWithoutRef & - VariantProps + VariantProps >(({ className, variant, ...props }, ref) => { return ( { // Initialize with first recently used model if available, otherwise empty return settings.recentlyUsedModels?.[0] || ''; @@ -41,6 +45,48 @@ export default function Index() { // Check if any providers are configured const hasProvidersConfigured = settings.providers.length > 0; + // Check for API key failures and show appropriate toast + const checkForKeyFailure = (error: unknown): boolean => { + if (!(error instanceof Error)) return false; + + const message = error.message.toLowerCase(); + const isAuthError = ( + message.includes('401') || + message.includes('unauthorized') || + message.includes('no auth credentials') || + message.includes('invalid api key') || + message.includes('authentication failed') + ); + + if (isAuthError) { + // Get provider name inline + let providerName = 'Provider'; + try { + const { provider } = parseProviderModel(providerModel, settings.providers); + providerName = provider.charAt(0).toUpperCase() + provider.slice(1); + } catch { + // Keep default 'Provider' + } + + toast({ + title: t('apiAuthenticationFailed'), + description: t('invalidApiKey', { provider: providerName }), + variant: 'destructive', + action: ( + navigate('/settings/ai')} + > + {t('checkApiKeySettings')} + + ), + }); + } + + return isAuthError; + }; + + useEffect(() => { if (!providerModel && settings.recentlyUsedModels?.length) { setProviderModel(settings.recentlyUsedModels[0]); @@ -185,6 +231,7 @@ export default function Index() { navigate(`/project/${project.id}?${searchParams.toString()}`); } catch (error) { console.error('Failed to create project:', error); + checkForKeyFailure(error); } finally { setIsCreating(false); } @@ -209,9 +256,8 @@ export default function Index() {
{/* Chat Input Container - matching the ChatPane style */}
Date: Tue, 23 Sep 2025 15:07:02 +0700 Subject: [PATCH 2/3] catch the error bubbled up exactly --- src/pages/Index.tsx | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index b35d3a02..6840eaae 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -13,6 +13,7 @@ import { OnboardingDialog } from '@/components/OnboardingDialog'; import { DotAI } from '@/lib/DotAI'; import { parseProviderModel } from '@/lib/parseProviderModel'; import type { AIMessage } from '@/lib/SessionManager'; +import { AuthenticationError } from 'openai'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; @@ -47,16 +48,9 @@ export default function Index() { // Check for API key failures and show appropriate toast const checkForKeyFailure = (error: unknown): boolean => { - if (!(error instanceof Error)) return false; - - const message = error.message.toLowerCase(); - const isAuthError = ( - message.includes('401') || - message.includes('unauthorized') || - message.includes('no auth credentials') || - message.includes('invalid api key') || - message.includes('authentication failed') - ); + // Check for authentication errors + // Note: useAIProjectId re-throws OpenAI errors as generic Error objects + const isAuthError = error instanceof Error && error.message === '401 No auth credentials found'; if (isAuthError) { // Get provider name inline From 8932413c2d52e159c5e7a21de255625d78982377 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 23 Sep 2025 21:08:54 +0700 Subject: [PATCH 3/3] lint fix --- src/pages/Index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 6840eaae..7ae2fc1e 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -13,7 +13,6 @@ import { OnboardingDialog } from '@/components/OnboardingDialog'; import { DotAI } from '@/lib/DotAI'; import { parseProviderModel } from '@/lib/parseProviderModel'; import type { AIMessage } from '@/lib/SessionManager'; -import { AuthenticationError } from 'openai'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea';