Skip to content

Commit b76b936

Browse files
committed
error handling
1 parent 916d16c commit b76b936

File tree

4 files changed

+41
-187
lines changed

4 files changed

+41
-187
lines changed

amplify/function/BusinessLogic/StartHackathon/handler.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Amplify } from "aws-amplify";
22
import { generateClient } from "aws-amplify/data";
3-
import { createHackathon, updateHackathon } from "@/amplify/graphql/mutations";
3+
import { createHackathon } from "@/amplify/graphql/mutations";
44
import { listHackathons } from "@/amplify/graphql/queries";
55
import type { Schema } from "../../../data/resource";
66

@@ -49,44 +49,29 @@ export const handler: Handler = async (event) => {
4949
const existingHackathons = hackathonItems?.listHackathons?.items || [];
5050

5151
if (existingHackathons.length > 0) {
52-
const hackathonId = existingHackathons[0].id;
53-
54-
const { errors } = await client.graphql({
55-
query: updateHackathon,
56-
variables: {
57-
input: {
58-
id: hackathonId,
59-
startDate: startDate,
60-
endDate: endDate,
61-
},
62-
},
63-
});
64-
65-
if (errors) throw errors;
66-
67-
console.log(
68-
`Hackathon updated to start on ${startDate} and end on ${endDate}`,
52+
throw new Error(
53+
"A hackathon already exists. Please reset the hackathon before starting a new one.",
6954
);
70-
} else {
71-
const { errors } = await client.graphql({
72-
query: createHackathon,
73-
variables: {
74-
input: {
75-
id: "1",
76-
startDate: startDate,
77-
endDate: endDate,
78-
scoringComponents: [],
79-
scoringSidepots: [],
80-
},
55+
}
56+
57+
const { errors } = await client.graphql({
58+
query: createHackathon,
59+
variables: {
60+
input: {
61+
id: "1",
62+
startDate: startDate,
63+
endDate: endDate,
64+
scoringComponents: [],
65+
scoringSidepots: [],
8166
},
82-
});
67+
},
68+
});
8369

84-
if (errors) throw errors;
70+
if (errors) throw errors;
8571

86-
console.log(
87-
`Hackathon created and scheduled to start on ${startDate} and end on ${endDate}`,
88-
);
89-
}
72+
console.log(
73+
`Hackathon created and scheduled to start on ${startDate} and end on ${endDate}`,
74+
);
9075

9176
return {
9277
statusCode: 200,

amplify/function/BusinessLogic/StopHackathon/handler.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

amplify/function/BusinessLogic/StopHackathon/resource.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/reset/ResetPage.tsx

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,54 +46,33 @@ export default function ResetPage() {
4646
});
4747

4848
const startHackathonMutation = useMutation({
49-
mutationFn: async ({
50-
startDate,
51-
endDate,
52-
}: {
53-
startDate: string;
54-
endDate: string;
55-
}) => {
56-
const toastObj = toast.loading("Scheduling hackathon start...");
57-
try {
58-
const response = await client.mutations.StartHackathon({
59-
startDate,
60-
endDate,
61-
});
62-
63-
console.log("StartHackathon response:", response);
49+
mutationFn: async (input: Schema["StartHackathon"]["args"]) => {
50+
console.log(input);
6451

65-
if (response.errors && response.errors.length > 0) {
66-
toast.dismiss(toastObj);
67-
const error = response.errors[0];
68-
console.error("GraphQL errors:", response.errors);
69-
const errorMsg =
70-
error.message || JSON.stringify(error) || "Unknown GraphQL error";
71-
toast.error(`Error: ${errorMsg}`);
72-
throw new Error(errorMsg);
52+
try {
53+
const existingHackathon = await client.models.Hackathon.list();
54+
if (existingHackathon.data && existingHackathon.data.length > 0) {
55+
toast.error("A hackathon already exists. Please reset first.");
56+
throw new Error("Hackathon already exists");
7357
}
7458

75-
if (response.data?.statusCode === 200) {
76-
toast.dismiss(toastObj);
77-
toast.success("Hackathon start scheduled successfully");
78-
await hackathonData.refetch();
79-
return response.data;
80-
} else {
59+
const toastObj = toast.loading("Scheduling hackathon start...");
60+
const { data: statusCode, errors } =
61+
await client.mutations.StartHackathon({
62+
...input,
63+
});
64+
if (errors) {
8165
toast.dismiss(toastObj);
82-
const respData = response.data
83-
? JSON.stringify(response.data)
84-
: "No data";
85-
console.error("Unexpected response:", response);
86-
toast.error(`Unexpected response: ${respData}`);
87-
throw new Error(`Unexpected response: ${respData}`);
66+
toast.error("Error scheduling hackathon start");
67+
console.log(errors);
8868
}
89-
} catch (error) {
69+
void statusCode;
9070
toast.dismiss(toastObj);
91-
console.error("Error scheduling hackathon start:", error);
92-
const errorMessage =
93-
error instanceof Error
94-
? error.message
95-
: JSON.stringify(error) || "Unknown error";
96-
toast.error(`Failed to schedule: ${errorMessage}`);
71+
toast.success("Hackathon start scheduled successfully");
72+
await hackathonData.refetch();
73+
return;
74+
} catch (error) {
75+
console.error("Error scheduling hackathon start", error);
9776
throw error;
9877
}
9978
},

0 commit comments

Comments
 (0)