From 12c9e75743e2044ad5b4e13223232709501cec37 Mon Sep 17 00:00:00 2001 From: Bailey Taylor Date: Fri, 10 Oct 2025 08:18:56 +0800 Subject: [PATCH] =?UTF-8?q?devices/page.tsx=20=E2=9C=85=20Added=20try-catc?= =?UTF-8?q?h=20block=20around=20the=20API=20call=20=E2=9C=85=20Proper=20er?= =?UTF-8?q?ror=20logging=20and=20meaningful=20error=20messages=20users/pag?= =?UTF-8?q?e.tsx=20=E2=9C=85=20Removed=20unused=20cookies()=20import=20and?= =?UTF-8?q?=20call=20(it=20was=20already=20being=20called=20inside=20requi?= =?UTF-8?q?reAuthOrRedirect())=20=E2=9C=85=20Added=20try-catch=20block=20a?= =?UTF-8?q?round=20the=20API=20call=20=E2=9C=85=20Proper=20error=20logging?= =?UTF-8?q?=20and=20meaningful=20error=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/(protected)/admin/devices/page.tsx | 19 +++++++++------ src/app/(protected)/admin/users/page.tsx | 28 +++++++++++----------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/app/(protected)/admin/devices/page.tsx b/src/app/(protected)/admin/devices/page.tsx index 012141b..67f731f 100644 --- a/src/app/(protected)/admin/devices/page.tsx +++ b/src/app/(protected)/admin/devices/page.tsx @@ -16,13 +16,18 @@ interface DeviceDTO { export default async function DeviceManagementPage() { const user = await requireAuthOrRedirect(); - const res = await axiosServer.get('/admin/devices', { - headers: { - Authorization: `Bearer ${user.token}`, - }, - }); + try { + const res = await axiosServer.get('/admin/devices', { + headers: { + Authorization: `Bearer ${user.token}`, + }, + }); - const devices: DeviceDTO[] = res.data; + const devices: DeviceDTO[] = res.data; - return ; + return ; + } catch (error) { + console.error('Failed to fetch devices:', error); + throw new Error(`Failed to load devices: ${error instanceof Error ? error.message : 'Unknown error'}`); + } } diff --git a/src/app/(protected)/admin/users/page.tsx b/src/app/(protected)/admin/users/page.tsx index f61388d..bd98e6c 100644 --- a/src/app/(protected)/admin/users/page.tsx +++ b/src/app/(protected)/admin/users/page.tsx @@ -1,5 +1,4 @@ // src/app/admin/users/page.tsx -import { cookies } from 'next/headers'; import { requireAuthOrRedirect } from '@/lib/authServer'; import axiosServer from '@/lib/axiosServer'; import UserTableSection from '@/components/admin/UserTableSection'; @@ -19,18 +18,19 @@ interface UserDTO { export default async function UserManagementPage() { const user = await requireAuthOrRedirect(); - const cookieStore = cookies(); - // @ts-expect-error - cookies() is not actually async, type is misleading - const token = cookieStore.get('authToken')?.value; - - const res = await axiosServer.get('/admin/users', { - headers: { - Authorization: `Bearer ${user.token}`, // token is guaranteed to exist - }, - }); - - const users: UserDTO[] = res.data; - - return ; + try { + const res = await axiosServer.get('/admin/users', { + headers: { + Authorization: `Bearer ${user.token}`, + }, + }); + + const users: UserDTO[] = res.data; + + return ; + } catch (error) { + console.error('Failed to fetch users:', error); + throw new Error(`Failed to load users: ${error instanceof Error ? error.message : 'Unknown error'}`); + } }