From 57618efc7f95f1396ba5e83dbe42b37516e91e17 Mon Sep 17 00:00:00 2001 From: Sefa Bulak Date: Thu, 28 Nov 2024 19:26:35 +0300 Subject: [PATCH] Improve error handling to use API messages in invite route - Updated the error handling to use GitHub API's error messages. - Improved status code handling for more accurate responses. - Simplified the code structure by merging try-catch blocks. --- .gitignore | 2 + app/api/invite/route.js | 137 +++++++++------------------------------- 2 files changed, 33 insertions(+), 106 deletions(-) diff --git a/.gitignore b/.gitignore index d086964..e5a8f81 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,5 @@ next-env.d.ts # Environment variables .env.* !.env.example +.env +.env.local \ No newline at end of file diff --git a/app/api/invite/route.js b/app/api/invite/route.js index 7224aa8..4be45aa 100644 --- a/app/api/invite/route.js +++ b/app/api/invite/route.js @@ -55,7 +55,7 @@ export async function POST(request) { return new NextResponse( JSON.stringify({ error: true, - message: "Github username is invaild!", + message: "Github username is invalid!", }), { status: 400, @@ -70,154 +70,79 @@ export async function POST(request) { auth: process.env.GITHUB_TOKEN, }); - if (!client) { - return new NextResponse( - JSON.stringify({ - error: true, - message: "Internal Server Error! Please try again later!", - }), - { - status: 500, - headers: { - "Content-Type": "application/json", - }, - } - ); - } - - const orgData = await client.orgs - .get({ + try { + const orgData = await client.orgs.get({ org: process.env.ORGANIZATION, - }) - .catch(() => { + }); + + const user = await client.users.getByUsername({ + username, + }); + + if (!user?.data?.id) { return new NextResponse( JSON.stringify({ error: true, - message: "Internal Server Error! Please try again later!", + message: "User not found!", }), { - status: 500, + status: 404, headers: { "Content-Type": "application/json", }, } ); - }); + } - if (orgData.status !== 200) { - return new NextResponse( - JSON.stringify({ - error: true, - message: "Internal Server Error! Please try again later!", - }), - { - status: 500, - headers: { - "Content-Type": "application/json", - }, - } - ); - } + const invite = await client.orgs.createInvitation({ + org: process.env.ORGANIZATION, + invitee_id: user.data.id, + role: "direct_member", + team_ids: [parseInt(process.env.TEAM_ID)], + }); - const user = await client.users - .getByUsername({ - username, - }) - .catch(() => { + if (invite.status !== 201) { return new NextResponse( JSON.stringify({ error: true, - message: "User not found!", + message: invite.data?.message || "Failed to send invitation!", }), { - status: 400, + status: invite.status || 400, headers: { "Content-Type": "application/json", }, } ); - }); + } - if (user.status !== 200) { return new NextResponse( JSON.stringify({ - error: true, - message: "User not found!", + error: false, + message: "User invited successfully!", }), { - status: 400, + status: 200, headers: { "Content-Type": "application/json", }, } ); - } + } catch (error) { + const apiErrorMessage = error?.response?.data?.message || "Internal Server Error!"; + const statusCode = error?.response?.status || 500; - const userData = user.data; - - if (!userData.id) { return new NextResponse( JSON.stringify({ error: true, - message: "User not found!", + message: apiErrorMessage, }), { - status: 400, + status: statusCode, headers: { "Content-Type": "application/json", }, } ); } - - const invite = await client.orgs - .createInvitation({ - org: process.env.ORGANIZATION, - invitee_id: userData.id, - role: "direct_member", - team_ids: [parseInt(process.env.TEAM_ID)], - }) - .catch((_err) => { - return new NextResponse( - JSON.stringify({ - error: true, - message: "Internal Server Error! Please try again later!", - }), - { - status: 400, - headers: { - "Content-Type": "application/json", - }, - } - ); - }); - - if (invite.status !== 201) { - const jsonBody = await invite.json(); - return new NextResponse( - JSON.stringify({ - error: true, - message: jsonBody?.message || "Internal Server Error! Please try again later!", - }), - { - status: 400, - headers: { - "Content-Type": "application/json", - }, - } - ); - } - - return new NextResponse( - JSON.stringify({ - error: false, - message: "User invited successfully!", - }), - { - status: 200, - headers: { - "Content-Type": "application/json", - }, - } - ); }