Skip to content

Commit 498d3cb

Browse files
committed
chore: improve user endpoint error handling in UI
1 parent 7b2f178 commit 498d3cb

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

src/service/routes/users.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ router.get('/:id', async (req: Request, res: Response) => {
1515
console.log(`Retrieving details for user: ${username}`);
1616
const user = await db.findUser(username);
1717
if (!user) {
18-
res.status(404).send('Error: User not found').end();
18+
res
19+
.status(404)
20+
.send({
21+
message: `User ${username} not found`,
22+
})
23+
.end();
1924
return;
2025
}
2126
res.send(toPublicUser(user));

src/ui/components/Navbars/DashboardNavbarLinks.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ const DashboardNavbarLinks: React.FC = () => {
2828
const [openProfile, setOpenProfile] = useState<HTMLElement | null>(null);
2929
const [, setAuth] = useState<boolean>(true);
3030
const [, setIsLoading] = useState<boolean>(true);
31-
const [, setIsError] = useState<boolean>(false);
31+
const [errorMessage, setErrorMessage] = useState<string>('');
3232
const [user, setUser] = useState<PublicUser | null>(null);
3333

3434
useEffect(() => {
35-
getUser(setIsLoading, setUser, setAuth, setIsError);
35+
getUser(setIsLoading, setUser, setAuth, setErrorMessage);
3636
}, []);
3737

3838
const handleClickProfile = (event: React.MouseEvent<HTMLElement>) => {
@@ -66,6 +66,7 @@ const DashboardNavbarLinks: React.FC = () => {
6666

6767
return (
6868
<div>
69+
{errorMessage && <div className={classes.errorMessage}>{errorMessage}</div>}
6970
<div className={classes.manager}>
7071
<Button
7172
color={window.innerWidth > 959 ? 'transparent' : 'white'}

src/ui/services/user.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const getUser = async (
1010
setIsLoading?: SetStateCallback<boolean>,
1111
setUser?: (user: PublicUser) => void,
1212
setAuth?: SetStateCallback<boolean>,
13-
setIsError?: SetStateCallback<boolean>,
13+
setErrorMessage?: SetStateCallback<string>,
1414
id: string | null = null,
1515
): Promise<void> => {
1616
let url = `${API_BASE}/api/auth/profile`;
@@ -26,10 +26,13 @@ const getUser = async (
2626
setIsLoading?.(false);
2727
} catch (error) {
2828
const axiosError = error as AxiosError;
29-
if (axiosError.response?.status === 401) {
29+
const status = axiosError.response?.status;
30+
if (status === 401) {
3031
setAuth?.(false);
32+
setErrorMessage?.(processAuthError(axiosError));
3133
} else {
32-
setIsError?.(true);
34+
const msg = (axiosError.response?.data as any)?.message ?? 'Unknown error';
35+
setErrorMessage?.(`Error fetching user: ${status} ${msg}`);
3336
}
3437
setIsLoading?.(false);
3538
}
@@ -50,32 +53,33 @@ const getUsers = async (
5053
);
5154
setUsers(response.data);
5255
} catch (error) {
53-
if (axios.isAxiosError(error)) {
54-
if (error.response?.status === 401) {
55-
setAuth(false);
56-
setErrorMessage(processAuthError(error));
57-
} else {
58-
const msg = (error.response?.data as any)?.message ?? error.message;
59-
setErrorMessage(`Error fetching users: ${msg}`);
60-
}
56+
const axiosError = error as AxiosError;
57+
const status = axiosError.response?.status;
58+
if (status === 401) {
59+
setAuth(false);
60+
setErrorMessage(processAuthError(axiosError));
6161
} else {
62-
setErrorMessage(`Error fetching users: ${(error as Error).message ?? 'Unknown error'}`);
62+
const msg = (axiosError.response?.data as any)?.message ?? 'Unknown error';
63+
setErrorMessage(`Error fetching users: ${status} ${msg}`);
6364
}
6465
} finally {
6566
setIsLoading(false);
6667
}
6768
};
6869

69-
const updateUser = async (user: PublicUser): Promise<void> => {
70-
console.log(user);
70+
const updateUser = async (
71+
user: PublicUser,
72+
setErrorMessage: SetStateCallback<string>,
73+
setIsLoading: SetStateCallback<boolean>,
74+
): Promise<void> => {
7175
try {
7276
await axios.post(`${API_BASE}/api/auth/gitAccount`, user, getAxiosConfig());
7377
} catch (error) {
7478
const axiosError = error as AxiosError;
75-
if (axiosError.response) {
76-
console.log((axiosError.response.data as any).message);
77-
}
78-
throw error;
79+
const status = axiosError.response?.status;
80+
const msg = (axiosError.response?.data as any)?.message ?? 'Unknown error';
81+
setErrorMessage(`Error updating user: ${status} ${msg}`);
82+
setIsLoading(false);
7983
}
8084
};
8185

src/ui/views/User/UserProfile.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function UserProfile(): React.ReactElement {
3131
const [user, setUser] = useState<PublicUser | null>(null);
3232
const [auth, setAuth] = useState<boolean>(true);
3333
const [isLoading, setIsLoading] = useState<boolean>(true);
34-
const [isError, setIsError] = useState<boolean>(false);
34+
const [errorMessage, setErrorMessage] = useState<string>('');
3535
const [gitAccount, setGitAccount] = useState<string>('');
3636
const navigate = useNavigate();
3737
const { id } = useParams<{ id?: string }>();
@@ -46,31 +46,27 @@ export default function UserProfile(): React.ReactElement {
4646
setGitAccount(user.gitAccount || '');
4747
},
4848
setAuth,
49-
setIsError,
49+
setErrorMessage,
5050
id,
5151
);
5252
}, [id]);
5353

5454
if (isLoading) return <div>Loading...</div>;
55-
if (isError) return <div>Something went wrong ...</div>;
55+
if (errorMessage) return <div>{errorMessage}</div>;
5656

5757
if (!auth && window.location.pathname === '/dashboard/profile') {
5858
return <Navigate to='/login' />;
5959
}
6060
if (!user) return <div>No user data available</div>;
6161

6262
const updateProfile = async (): Promise<void> => {
63-
try {
64-
const updatedData = {
65-
...user,
66-
gitAccount: escapeHTML(gitAccount),
67-
};
68-
await updateUser(updatedData);
69-
setUser(updatedData);
70-
navigate(`/dashboard/profile`);
71-
} catch {
72-
setIsError(true);
73-
}
63+
const updatedData = {
64+
...user,
65+
gitAccount: escapeHTML(gitAccount),
66+
};
67+
await updateUser(updatedData, setErrorMessage, setIsLoading);
68+
setUser(updatedData);
69+
navigate(`/dashboard/profile`);
7470
};
7571

7672
const UpdateButton = (): React.ReactElement => (

0 commit comments

Comments
 (0)