Skip to content

Commit e2129c5

Browse files
committed
error handling added
1 parent 91c8b6d commit e2129c5

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require("./config")(app) // waiting for config
1111
const indexRoutes = require("./routes/index.routes")
1212
app.use("/api", indexRoutes)
1313

14-
// require("./error-handling")(app) // waiting for error-handling
14+
require("./error-handling")(app) // waiting for error-handling
1515

1616
// // TEST
1717
// app.get("/", (req, res) => {

error-handling/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = (app) => {
2+
app.use((req, res, next) => {
3+
res.status(404).json({ message: "This route does not exist" });
4+
});
5+
6+
app.use((err, req, res, next) => {
7+
console.error("ERROR", req.method, req.path, err);
8+
9+
if (!res.headersSent) {
10+
res.status(500).json({
11+
message: "Internal server error. Check the server console",
12+
});
13+
}
14+
});
15+
};

routes/user.routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ router.get("/user-profile/:userid", verifyToken, async (req, res) => {
5858
createdRecs: user.createdRecs
5959
});
6060
} catch (error) {
61-
res.status(500).json({ message: "Error fetching user data" });
61+
next(error)
6262
}
6363
});
6464

0 commit comments

Comments
 (0)