Skip to content

Commit

Permalink
adressed mentor's comments: updated firstMeal and lastMeal to correct…
Browse files Browse the repository at this point in the history
…ly return the meal objects. Updated error handling middleware.
  • Loading branch information
abuniatov committed Oct 23, 2024
1 parent 45c578e commit 4e61968
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ app.get("/meals/first-meal", async (req, res, next) => {
error.status = 404;
return next(error);
}
res.json(meal);
res.json(firstMeal);
} catch (error) {
next(error);
}
Expand All @@ -71,7 +71,7 @@ app.get("/meals/last-meal", async (req, res, next) => {
error.status = 404;
return next(error);
}
res.json(meal);
res.json(lastMeal);
} catch (error) {
next(error);
}
Expand All @@ -84,11 +84,16 @@ app.use("/api", apiRouter);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);

if (err.status === 404) {
return res.status(404).json({ error: "Resource not found" });
}

res.status(err.status || 500).json({
error: err.message || "Internal Server Error",
error: "An error occurred. Please try again later.",
});
});

app.listen(process.env.PORT, () => {
console.log(`API listening on port ${process.env.PORT}`);
});
});

0 comments on commit 4e61968

Please sign in to comment.