From de0340fabcd255560c5a95d292894f6f485b9c94 Mon Sep 17 00:00:00 2001 From: Melissa Hale Date: Wed, 17 Jul 2024 20:55:26 -0500 Subject: [PATCH] error handling --- src/pages/api/community/getThread.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/pages/api/community/getThread.ts b/src/pages/api/community/getThread.ts index e6544fb05..3476da5b0 100644 --- a/src/pages/api/community/getThread.ts +++ b/src/pages/api/community/getThread.ts @@ -6,11 +6,18 @@ export interface CommunitySlug { } export default async (req: NextApiRequest, res: NextApiResponse) => { - if (req.method == "POST") { - const { communitySlug }: CommunitySlug = req.body; + const { communitySlug }: CommunitySlug = req.body; - const { data } = await communityClient.thread({ slug: communitySlug }); + try { + const response = await communityClient.thread({ slug: communitySlug }); - res.status(200).json(data); + if (!response || !response.data) { + return res.status(400).json({ error: 'Error fetching thread data' }); + } + + res.status(200).json(response.data); + } catch (error) { + console.error('Error fetching thread data:', error); + res.status(500).json({ error: 'Error fetching thread data' }); } };