@@ -25,6 +25,7 @@ import { BadgeCheck, Target, TrendingUp } from 'lucide-react'
2525import { Switch } from '@headlessui/react'
2626import AcademicHistory from '@/components/AcademicHistory'
2727import LoginHistory from '@/components/LoginHistory'
28+ import ErrorBoundary from '@/components/ErrorBoundary'
2829
2930if ( ! firebase . apps . length ) {
3031 firebase . initializeApp ( firebaseConfig )
@@ -747,18 +748,29 @@ function HomePage() {
747748 apiurl = 'https://reconnect-msrit.vercel.app/test'
748749 }
749750 const response = await fetch ( apiurl )
750- if ( ! response . ok ) {
751- if ( response . status === 500 ) {
752- const error = new Error (
753- 'Server error: This endpoint is currently inactive. Try switching the semester toggle to use a different endpoint.'
754- )
755- error . isEndpointError = true
756- throw error
751+ let data ;
752+ try {
753+ if ( ! response . ok ) {
754+ if ( response . status === 500 ) {
755+ const error = new Error (
756+ 'Server error: This endpoint is currently inactive. Try switching the semester toggle to use a different endpoint.'
757+ )
758+ error . isEndpointError = true
759+ // Throw error to be caught by error boundary
760+ throw error
761+ }
762+ const resp = await response . json ( )
763+ throw new Error ( resp . error || 'Failed to fetch data.' )
757764 }
758- const resp = await response . json ( )
759- throw new Error ( resp . error || 'Failed to fetch data.' )
765+ data = await response . json ( )
766+ } catch ( error ) {
767+ // Set error state and clear data
768+ setStudentData ( null )
769+ setError ( error . message || 'An unexpected error occurred' )
770+ setHasError ( true )
771+ // Re-throw the error to be caught by error boundary
772+ throw error
760773 }
761- const data = await response . json ( )
762774 localStorage . setItem ( 'usn' , currentUsn )
763775 localStorage . setItem ( 'dob' , currentDob )
764776 localStorage . setItem ( 'studentData' , JSON . stringify ( data ) )
@@ -1068,6 +1080,15 @@ function HomePage() {
10681080 ) : (
10691081 < section className = "flex w-full items-center justify-center bg-indigo-50 pb-8 dark:bg-gray-900 sm:py-2" >
10701082 < div className = "max-w-3xl lg:mx-auto lg:w-full" >
1083+ < ErrorBoundary
1084+ enabled = { enabled }
1085+ onToggle = { ( value ) => {
1086+ setEnabled ( value )
1087+ localStorage . setItem ( 'semesterToggle' , value )
1088+ handleFetchData ( usn , dob )
1089+ } }
1090+ onRetry = { ( ) => handleFetchData ( usn , dob ) }
1091+ >
10711092 { studentData && (
10721093 < >
10731094 < div className = "my-2 rounded-md shadow-md dark:bg-gray-800" >
@@ -1255,6 +1276,7 @@ function HomePage() {
12551276 ) }
12561277 </ >
12571278 ) }
1279+ </ ErrorBoundary >
12581280 < div className = "mt-4 flex justify-center gap-4" >
12591281 < button
12601282 onClick = { handleReload }
0 commit comments