Skip to content

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

feat(studio): link edge function errors to troubleshooting docs (#45326)

Jordi Enricfe928ad76d4d
diff --git a/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.tsx b/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.tsx
index 167a5da2e23..7d9cdc5c811 100644
--- a/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.tsx
+++ b/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.tsx
@@ -1,4 +1,4 @@
-import { Check, ExternalLink, Eye } from 'lucide-react'
+import { BookOpen, Check, ExternalLink, Eye } from 'lucide-react'
import { useRouter } from 'next/router'
import { Fragment, useMemo } from 'react'
import {
@@ -26,8 +26,10 @@ import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import {
buildGroupAssistantPrompt,
+ buildTroubleshootingDocsUrl,
formatLogTimestamp,
formatSingleLineMessage,
+ getDisplayErrorMessage,
getFunctionRuntimeLogsSql,
getRecentErrorGroups,
getRecentErrorGroupsBase,
@@ -242,85 +244,121 @@ export const EdgeFunctionRecentErrors = ({
<TableHead>Method</TableHead>
<TableHead>Status</TableHead>
<TableHead>Duration</TableHead>
- <TableHead className="text-right">Assistant</TableHead>
+ <TableHead className="text-right">Troubleshoot</TableHead>
</TableRow>
</TableHeader>
<TableBody>
- {recentErrorGroups.map((group) => (
- <Fragment key={group.message}>
- <TableRow key={`${group.message}-summary`}>
- <TableCell className="max-w-[420px]">
- <span
- className="block truncate whitespace-nowrap text-foreground"
- title={formatSingleLineMessage(group.message)}
- >
- {formatSingleLineMessage(group.message)}
- </span>
- </TableCell>
- <TableCell className="text-foreground-light">{group.count}</TableCell>
- <TableCell className="text-foreground-light">
- {formatLogTimestamp(group.lastSeen, 'relative')}
- </TableCell>
- <TableCell className="text-foreground-light">
- {group.lastMethod ?? '-'}
- </TableCell>
- <TableCell>
- {group.lastStatusCode ? (
- <Badge
- variant={getStatusBadgeVariant(group.lastStatusCode)}
- className="font-mono"
+ {recentErrorGroups.map((group) => {
+ const displayMessage = getDisplayErrorMessage(group)
+ const docsUrl = buildTroubleshootingDocsUrl({
+ statusCode: group.lastStatusCode,
+ })
+
+ return (
+ <Fragment key={group.message}>
+ <TableRow key={`${group.message}-summary`}>
+ <TableCell className="max-w-[420px]">
+ <span
+ className="block truncate whitespace-nowrap text-foreground"
+ title={displayMessage}
>
- {group.lastStatusCode}
- </Badge>
- ) : (
- <Badge variant="destructive" className="font-mono">
- Error
- </Badge>
- )}
- </TableCell>
- <TableCell className="text-foreground-light">
- {group.executionTime ?? '-'}
- </TableCell>
- <TableCell className="text-right">
- <div className="flex justify-end">
- <AiAssistantDropdown
- label="Ask Assistant"
- size="tiny"
- buildPrompt={() => buildGroupAssistantPrompt(group, functionSlug)}
- onOpenAssistant={() => handleOpenAssistant(group)}
- />
- </div>
- </TableCell>
- </TableRow>
- <TableRow key={`${group.message}-logs`} className="bg-surface-100/30">
- <TableCell colSpan={7} className="p-0">
- <div className="max-h-64 overflow-auto bg-surface-75 py-3 text-foreground-light">
- {group.logs.length === 0 ? (
- <div className="px-4">
- No related runtime logs found for this error group.
- </div>
+ {displayMessage}
+ </span>
+ </TableCell>
+ <TableCell className="text-foreground-light">{group.count}</TableCell>
+ <TableCell className="text-foreground-light">
+ {formatLogTimestamp(group.lastSeen, 'relative')}
+ </TableCell>
+ <TableCell className="text-foreground-light">
+ {group.lastMethod ?? '-'}
+ </TableCell>
+ <TableCell>
+ {group.lastStatusCode ? (
+ <Badge
+ variant={getStatusBadgeVariant(group.lastStatusCode)}
+ className="font-mono"
+ >
+ {group.lastStatusCode}
+ </Badge>
) : (
- group.logs.map((log) => (
- <div
- key={log.key}
- className={cn(
- 'break-words px-4',
- log.level === 'error'
- ? 'bg-destructive-300 font-mono text-destructive'
- : 'text-foreground-light'
- )}
- >
- [{formatLogTimestamp(log.lastSeen, 'time')}] [
- {log.level.toUpperCase()}] [{log.count}x]{' '}
- {formatSingleLineMessage(log.message)}
- </div>
- ))
+ <Badge variant="destructive" className="font-mono">
+ Error
+ </Badge>
)}
- </div>
- </TableCell>
- </TableRow>
- </Fragment>
- ))}
+ </TableCell>
+ <TableCell className="text-foreground-light">
+ {group.executionTime ?? '-'}
+ </TableCell>
+ <TableCell className="text-right">
+ <div className="flex justify-end">
+ <AiAssistantDropdown
+ label="Ask Assistant"
+ size="tiny"
+ buildPrompt={() => buildGroupAssistantPrompt(group, functionSlug)}
+ onOpenAssistant={() => handleOpenAssistant(group)}
+ additionalDropdownItems={[
+ {
+ label: 'View troubleshooting guide',
+ icon: <BookOpen size={14} />,
+ onClick: () =>
+ window.open(docsUrl, '_blank', 'noopener,noreferrer'),
+ },
+ ]}
+ />
+ </div>
+ </TableCell>
+ </TableRow>
+ <TableRow key={`${group.message}-logs`} className="hover:bg-transparent">
+ <TableCell colSpan={7} className="p-0">
+ <div className="max-h-64 overflow-auto bg-surface-75 font-mono text-xs">
+ {group.logs.length === 0 ? (
+ <div className="px-4 py-3 text-foreground-lighter">
+ No related runtime logs found for this error group.
+ </div>
+ ) : (
+ group.logs.map((log, index) => {
+ const isError = log.level === 'error'
+ return (
+ <div
+ key={log.key}
+ className={cn(
+ 'flex items-start gap-3 px-4 py-2',
+ index !== 0 && 'border-t border-default',
+ isError && 'bg-destructive-200/40'
+ )}
+ >
+ <span className="shrink-0 tabular-nums text-foreground-muted">
+ {formatLogTimestamp(log.lastSeen, 'time')}
+ </span>
+ <Badge
+ variant={isError ? 'destructive' : 'default'}
+ className="shrink-0"
+ >
+ {log.level}
+ </Badge>
+ {log.count > 1 && (
+ <span className="shrink-0 text-foreground-muted tabular-nums">
+ ×{log.count}
+ </span>
+ )}
+ <span
+ className={cn(
+ 'flex-1 break-words whitespace-pre-wrap',
+ isError ? 'text-destructive' : 'text-foreground-light'
+ )}
+ >
+ {formatSingleLineMessage(log.message)}
+ </span>
+ </div>
+ )
+ })
+ )}
+ </div>
+ </TableCell>
+ </TableRow>
+ </Fragment>
+ )
+ })}
</TableBody>
</Table>
</Card>
diff --git a/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.test.ts b/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.test.ts
index 85981690937..ae651686165 100644
--- a/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.test.ts
+++ b/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.test.ts
@@ -2,8 +2,10 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import {
buildGroupAssistantPrompt,
+ buildTroubleshootingDocsUrl,
formatLogTimestamp,
formatSingleLineMessage,
+ getDisplayErrorMessage,
getFunctionRuntimeLogsSql,
getNoErrorsSinceLastDeployMessage,
getRecentErrorGroups,
@@ -14,6 +16,7 @@ import {
getSinceLastDeployInvocationPhrase,
getSinceLastDeployLogRange,
getStatusBadgeVariant,
+ summarizeErrorMessage,
toAlertError,
toIsoTimestamp,
} from './EdgeFunctionRecentErrors.utils'
@@ -267,4 +270,67 @@ limit 25`)
expect(getStatusBadgeVariant('500')).toBe('destructive')
expect(getStatusBadgeVariant('404')).toBe('default')
})
+
+ it('summarizes verbose error messages by trimming the stack trace', () => {
+ expect(summarizeErrorMessage('')).toBe('')
+ expect(summarizeErrorMessage('boom')).toBe('boom')
+ expect(
+ summarizeErrorMessage(
+ "SyntaxError: Expected ',' or '}' after property value in JSON at position 22 at parse (<anonymous>) at packageData (ext:deno_fetch/22_body.js:408:14)"
+ )
+ ).toBe("SyntaxError: Expected ',' or '}' after property value in JSON at position 22")
+ expect(summarizeErrorMessage(' multi\n line\t error ')).toBe('multi line error')
+ })
+
+ it('prefers the first runtime error log message and falls back to invocation message', () => {
+ expect(
+ getDisplayErrorMessage({
+ message: 'https://example.supabase.red/functions/v1/hello-world',
+ count: 1,
+ lastSeen: 0,
+ executionIds: [],
+ logs: [
+ {
+ key: 'log:booted (time: 22ms)',
+ message: 'booted (time: 22ms)',
+ level: 'log',
+ count: 1,
+ lastSeen: 1,
+ },
+ {
+ key: 'error:SyntaxError: bad json at parse (<anonymous>)',
+ message: 'SyntaxError: bad json at parse (<anonymous>)',
+ level: 'error',
+ count: 1,
+ lastSeen: 2,
+ },
+ ],
+ })
+ ).toBe('SyntaxError: bad json')
+
+ expect(
+ getDisplayErrorMessage({
+ message: 'https://example.supabase.red/functions/v1/hello-world',
+ count: 1,
+ lastSeen: 0,
+ executionIds: [],
+ logs: [],
+ })
+ ).toBe('https://example.supabase.red/functions/v1/hello-world')
+ })
+
+ it('builds a troubleshooting docs URL keyed off the response status code', () => {
+ expect(buildTroubleshootingDocsUrl({ statusCode: '500' })).toBe(
+ 'https://supabase.com/docs/guides/troubleshooting/edge-function-500-response'
+ )
+ expect(buildTroubleshootingDocsUrl({ statusCode: '503' })).toBe(
+ 'https://supabase.com/docs/guides/troubleshooting/edge-function-503-response'
+ )
+ expect(buildTroubleshootingDocsUrl({})).toBe(
+ 'https://supabase.com/docs/guides/troubleshooting?search=edge%20function'
+ )
+ expect(buildTroubleshootingDocsUrl({ statusCode: 'not-a-number' })).toBe(
+ 'https://supabase.com/docs/guides/troubleshooting?search=edge%20function'
+ )
+ })
})
diff --git a/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.ts b/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.ts
index 9bf64efc237..d404963428b 100644
--- a/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.ts
+++ b/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.utils.ts
@@ -45,6 +45,46 @@ export const escapeSqlString = (value: string) => value.replace(/'/g, "''")
export const formatSingleLineMessage = (message: string) => message.replace(/\s+/g, ' ').trim()
+/**
+ * Trims a runtime error message down to the meaningful summary, dropping the
+ * stack trace that follows the first ` at ` frame so we can show it inline in
+ * a table cell.
+ */
+export const summarizeErrorMessage = (message: string): string => {
+ const collapsed = formatSingleLineMessage(message)
+ if (!collapsed) return collapsed
+
+ const stackFrameMatch = collapsed.match(/\s+at\s+\S+\s+\(/)
+ if (stackFrameMatch && stackFrameMatch.index !== undefined) {
+ return collapsed.slice(0, stackFrameMatch.index).trim()
+ }
+ return collapsed
+}
+
+/**
+ * Picks the most useful error description for a group. The invocation
+ * `event_message` only contains the request URL, so when we have a related
+ * runtime error log we surface its summary instead.
+ */
+export const getDisplayErrorMessage = (group: RecentErrorGroup): string => {
+ const errorLog = group.logs.find((log) => log.level === 'error')
+ if (errorLog) {
+ const summary = summarizeErrorMessage(errorLog.message)
+ if (summary) return summary
+ }
+ return summarizeErrorMessage(group.message)
+}
+
+const TROUBLESHOOTING_DOCS_BASE = 'https://supabase.com/docs/guides/troubleshooting'
+
+export const buildTroubleshootingDocsUrl = ({ statusCode }: { statusCode?: string }): string => {
+ const numericStatusCode = Number(statusCode)
+ if (Number.isFinite(numericStatusCode) && numericStatusCode >= 100) {
+ return `${TROUBLESHOOTING_DOCS_BASE}/edge-function-${numericStatusCode}-response`
+ }
+ return `${TROUBLESHOOTING_DOCS_BASE}?search=${encodeURIComponent('edge function')}`
+}
+
export const toAlertError = (error: unknown): AlertErrorProps['error'] | undefined => {
if (typeof error === 'string') return { message: error }