Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling to use API messages in invite route #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ next-env.d.ts
# Environment variables
.env.*
!.env.example
.env
.env.local
137 changes: 31 additions & 106 deletions app/api/invite/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
return new NextResponse(
JSON.stringify({
error: true,
message: "Github username is invaild!",
message: "Github username is invalid!",
}),
{
status: 400,
Expand All @@ -70,154 +70,79 @@
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({

Check warning on line 74 in app/api/invite/route.js

View workflow job for this annotation

GitHub Actions / ESLint (lts/*)

'orgData' is assigned a value but never used. Allowed unused vars must match /^_/u
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",
},
}
);
}