feat: improve global error reporting with user notifs

This commit is contained in:
Jake Turner 2026-02-04 22:12:11 -08:00 committed by Jake Turner
parent 52e90041f4
commit fcc749ec57
2 changed files with 28 additions and 1 deletions

View File

@ -1,3 +1,11 @@
import { Notification } from "~/context/NotificationContext"
// Global notification callback that can be set by the NotificationProvider
let globalNotificationCallback: ((notification: Notification) => void) | null = null
export function setGlobalNotificationCallback(callback: (notification: Notification) => void) {
globalNotificationCallback = callback
}
export function capitalizeFirstLetter(str?: string | null): string {
if (!str) return ''
@ -68,6 +76,16 @@ export function catchInternal<Fn extends (...args: any[]) => any>(fn: Fn): (...a
return await fn(...args)
} catch (error) {
console.error('Internal error caught:', error)
if (globalNotificationCallback) {
const errorMessage = 'An internal error occurred. Please try again or check the console for details. ' + (error instanceof Error ? String(error.message).slice(0, 50) : '')
globalNotificationCallback({
message: errorMessage,
type: 'error',
duration: 5000
})
}
return undefined
}
}

View File

@ -1,6 +1,7 @@
import { useState } from 'react'
import { useState, useEffect } from 'react'
import { NotificationContext, Notification } from '../context/NotificationContext'
import { IconExclamationCircle, IconCircleCheck, IconInfoCircle } from '@tabler/icons-react'
import { setGlobalNotificationCallback } from '~/lib/util'
const NotificationsProvider = ({ children }: { children: React.ReactNode }) => {
const [notifications, setNotifications] = useState<(Notification & { id: string })[]>([])
@ -17,6 +18,14 @@ const NotificationsProvider = ({ children }: { children: React.ReactNode }) => {
}
}
// Set the global notification callback when provider mounts
useEffect(() => {
setGlobalNotificationCallback(addNotification)
return () => {
setGlobalNotificationCallback(() => {})
}
}, [])
const removeNotification = (id: string) => {
setNotifications((prev) => prev.filter((n) => n.id !== id))
}