Skip to content

Commit 9517b59

Browse files
committed
fix: add safeStringify to prevent crash on circular refs
CodeRabbit feedback - JSON.stringify can throw on circular objects. Wrap in try-catch, fallback to String() on error.
1 parent 4dbc963 commit 9517b59

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

frontend/src/components/dashboard/DashboardHome.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ import { API_URL } from '../../config/api'
3333

3434
const MAX_FREE_REPOS = 3
3535

36+
// Safe stringify that won't crash on circular refs
37+
function safeStringify(obj: unknown, maxLen = 200): string {
38+
try {
39+
return JSON.stringify(obj).slice(0, maxLen)
40+
} catch {
41+
return String(obj).slice(0, maxLen)
42+
}
43+
}
44+
3645
// Extract error message from API response (handles nested detail objects)
3746
function extractErrorMessage(err: any, fallback: string): string {
3847
// FastAPI wraps in detail, but handle both cases
@@ -42,11 +51,11 @@ function extractErrorMessage(err: any, fallback: string): string {
4251
if (typeof detail?.message === 'string') return detail.message
4352
if (typeof err?.message === 'string') return err.message
4453

45-
// Last resort: stringify (but keep it short)
54+
// Last resort: stringify (but keep it short, safe from circular refs)
4655
if (detail && typeof detail === 'object') {
4756
const msg = detail.message || detail.error
4857
if (msg) return String(msg)
49-
return JSON.stringify(detail).slice(0, 200)
58+
return safeStringify(detail)
5059
}
5160
return fallback
5261
}

0 commit comments

Comments
 (0)