Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add error boundary to AI Assistant (#35850)

Jordi Enric88066cec591e
diff --git a/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx b/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx
index 12baaabfcd5..b1db254cf7f 100644
--- a/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx
+++ b/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx
@@ -46,6 +46,7 @@ import AIOnboarding from './AIOnboarding'
import CollapsibleCodeBlock from './CollapsibleCodeBlock'
import { Message } from './Message'
import { useAutoScroll } from './hooks'
+import { ErrorBoundary } from '../ErrorBoundary'
const MemoizedMessage = memo(
({
@@ -304,7 +305,24 @@ export const AIAssistant = ({ className }: AIAssistantProps) => {
}, [snap.open, isInSQLEditor, snippetContent])
return (
- <>
+ <ErrorBoundary
+ message="Something went wrong with the AI Assistant"
+ sentryContext={{
+ component: 'AIAssistant',
+ feature: 'AI Assistant Panel',
+ projectRef: project?.ref,
+ organizationSlug: selectedOrganization?.slug,
+ }}
+ actions={[
+ {
+ label: 'Clear messages and refresh',
+ onClick: () => {
+ handleClearMessages()
+ window.location.reload()
+ },
+ },
+ ]}
+ >
<div className={cn('flex flex-col h-full', className)}>
<div ref={scrollContainerRef} className={cn('flex-grow overflow-auto flex flex-col')}>
<div className="z-30 sticky top-0">
@@ -639,6 +657,6 @@ export const AIAssistant = ({ className }: AIAssistantProps) => {
<OptInToOpenAIToggle />
</ConfirmationModal>
- </>
+ </ErrorBoundary>
)
}
diff --git a/apps/studio/components/ui/ErrorBoundary.tsx b/apps/studio/components/ui/ErrorBoundary.tsx
new file mode 100644
index 00000000000..ac82ea58da4
--- /dev/null
+++ b/apps/studio/components/ui/ErrorBoundary.tsx
@@ -0,0 +1,97 @@
+import { ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary'
+import * as Sentry from '@sentry/nextjs'
+import { Alert_Shadcn_, AlertDescription_Shadcn_, AlertTitle_Shadcn_, Button } from 'ui'
+import { AlertCircle } from 'lucide-react'
+import { ErrorInfo } from 'react'
+
+interface ErrorFallbackProps {
+ error: Error
+ resetErrorBoundary: () => void
+ message?: string
+ actions?: {
+ label: string
+ onClick: () => void
+ }[]
+ sentryContext?: Record<string, any>
+}
+
+const ErrorFallback = ({
+ error,
+ resetErrorBoundary,
+ message = 'Something went wrong',
+ actions = [],
+}: ErrorFallbackProps) => {
+ return (
+ <div className="p-4 bg-destructive-foreground h-full flex flex-col justify-center items-center">
+ <Alert_Shadcn_ variant="destructive">
+ <AlertCircle />
+ <AlertTitle_Shadcn_>{message}</AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ We've been notified and will review and fix this issue.
+ </AlertDescription_Shadcn_>
+ <div className="mt-4 flex gap-2">
+ <Button type="default" onClick={resetErrorBoundary} className="text-sm">
+ Try again
+ </Button>
+ {actions?.map((action, index) => (
+ <Button key={index} type="default" onClick={action.onClick} className="text-sm">
+ {action.label}
+ </Button>
+ ))}
+ </div>
+ </Alert_Shadcn_>
+ </div>
+ )
+}
+
+interface ErrorBoundaryProps {
+ children: React.ReactNode
+ message?: string
+ actions?: {
+ label: string
+ onClick: () => void
+ }[]
+ sentryContext?: Record<string, any>
+ onReset?: () => void
+}
+
+export const ErrorBoundary = ({
+ children,
+ message,
+ actions,
+ sentryContext,
+ onReset,
+}: ErrorBoundaryProps) => {
+ const handleError = (error: Error, info: ErrorInfo) => {
+ Sentry.withScope((scope) => {
+ scope.setExtra('componentStack', info.componentStack)
+ if (sentryContext) {
+ Object.entries(sentryContext).forEach(([key, value]) => {
+ scope.setExtra(key, value)
+ })
+ }
+ Sentry.captureException(error)
+ })
+ }
+
+ const handleReset = () => {
+ onReset?.()
+ }
+
+ return (
+ <ReactErrorBoundary
+ fallbackRender={({ error, resetErrorBoundary }) => (
+ <ErrorFallback
+ error={error}
+ resetErrorBoundary={resetErrorBoundary}
+ message={message}
+ actions={actions}
+ />
+ )}
+ onError={handleError}
+ onReset={handleReset}
+ >
+ {children}
+ </ReactErrorBoundary>
+ )
+}