Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 1c8754b

Browse files
Add error handling for user creation and improve server initialization
1 parent 2bdbfb7 commit 1c8754b

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

api/controllers/createUser.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
const User = require("../models/User.js");
22

33
const createUser = async (repos) => {
4-
const username = repos[0].owner.login;
5-
const githubId = repos[0].owner.id;
6-
const avatarUrl = repos[0].owner.avatar_url;
4+
if (!repos || repos.length === 0) {
5+
throw new Error("No repositories found for the user.");
6+
}
7+
8+
const username = repos[0]?.owner?.login;
9+
const githubId = repos[0]?.owner?.id;
10+
const avatarUrl = repos[0]?.owner?.avatar_url;
11+
12+
if (!username || !githubId || !avatarUrl) {
13+
throw new Error("Owner information is missing in the repository data.");
14+
}
715

816
const existingUser = await User.findOne({ username });
917

@@ -12,7 +20,6 @@ const createUser = async (repos) => {
1220
return existingUser;
1321
}
1422

15-
// Create new user
1623
const newUser = await User.create({
1724
githubId,
1825
username,
@@ -21,10 +28,11 @@ const createUser = async (repos) => {
2128
name: repo.name,
2229
url: repo.html_url,
2330
})),
24-
accessToken: "", // Store access token if needed
31+
accessToken: "",
2532
});
2633

2734
console.log("✅ New user created with username:", username);
35+
return newUser;
2836
};
2937

3038
module.exports = createUser;

api/server.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,37 @@ const analyzeRoutes = require("./routes/analyze");
1313

1414
const app = express();
1515

16-
app.use(cors());
17-
app.use(express.json());
18-
app.use(
19-
session({
20-
secret: process.env.SESSION_SECRET,
21-
resave: false,
22-
saveUninitialized: true,
23-
})
24-
);
25-
app.use(passport.initialize());
26-
app.use(passport.session());
16+
try {
17+
app.use(cors());
18+
app.use(express.json());
19+
app.use(
20+
session({
21+
secret: process.env.SESSION_SECRET,
22+
resave: false,
23+
saveUninitialized: true,
24+
})
25+
);
26+
app.use(passport.initialize());
27+
app.use(passport.session());
2728

28-
app.use("/auth", authRoutes);
29-
app.use("/github", githubRoutes);
30-
app.use("/", analyzeRoutes);
29+
app.use("/auth", authRoutes);
30+
app.use("/github", githubRoutes);
31+
app.use("/", analyzeRoutes);
3132

32-
mongoose
33-
.connect(process.env.MONGO_URI)
34-
.then(() => console.log("MongoDB Connected"))
35-
.catch((err) => console.error("MongoDB Connection Error:", err));
33+
mongoose
34+
.connect(process.env.MONGO_URI)
35+
.then(() => console.log("MongoDB Connected"))
36+
.catch((err) => console.error("MongoDB Connection Error:", err));
3637

37-
const PORT = process.env.API_PORT || 5000;
38-
app.listen(PORT, () => {
39-
console.log(`Server running on http://localhost:${PORT}`);
40-
console.log(
41-
`GitHub API status: ${
42-
process.env.GITHUB_TOKEN ? "Authenticated" : "Not configured"
43-
}`
44-
);
45-
});
38+
const PORT = process.env.API_PORT || 5000;
39+
app.listen(PORT, () => {
40+
console.log(`Server running on http://localhost:${PORT}`);
41+
console.log(
42+
`GitHub API status: ${
43+
process.env.GITHUB_TOKEN ? "Authenticated" : "Not configured"
44+
}`
45+
);
46+
});
47+
} catch (error) {
48+
console.error("An error occurred while starting the server:", error);
49+
}

0 commit comments

Comments
 (0)