Skip to content

Commit 98c2bab

Browse files
author
nickbar01234
committed
Handle errors
1 parent dd0f5f6 commit 98c2bab

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

src/Controllers/fetchUserDetails.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const fetchUserDetails = async <T, U>(
3636
return res.json(formatData(result.data));
3737
} catch (err) {
3838
console.error('Error: ', err);
39-
return res.send(err);
39+
return res.send(err.message);
4040
}
4141
};
4242

src/FormatUtils/formatter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ import { ZodType } from 'zod';
22

33
export const withSchema =
44
<T, U>(schema: ZodType<T>, formatter: (data: T) => U) =>
5-
(input: T) =>
6-
formatter(schema.parse(input));
5+
(input: T) => {
6+
const result = schema.safeParse(input);
7+
if (result.success) {
8+
return formatter(result.data);
9+
}
10+
throw new Error(result.error.message);
11+
};

src/schema/common/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import z from 'zod';
22

33
export const badge = z.object({
44
name: z.string(),
5-
icon: z.string(),
5+
icon: z.string().optional(),
66
});

src/schema/contests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { badge } from './common';
33

44
const userContestRanking = z.object({
55
attendedContestsCount: z.number().nonnegative(),
6-
badge,
6+
badge: badge.nullable(),
77
globalRanking: z.number().nonnegative(),
88
rating: z.number().nonnegative(),
99
totalParticipants: z.number().nonnegative(),
@@ -20,7 +20,7 @@ const userContestRankingHistory = z.object({
2020
finishTimeInSeconds: z.number().nonnegative(),
2121
contest: z.object({
2222
title: z.string(),
23-
startTime: z.string(),
23+
startTime: z.number(),
2424
}),
2525
});
2626

0 commit comments

Comments
 (0)