better error handling

This commit is contained in:
Maxime Cannoodt 2022-07-06 15:07:51 +02:00
parent 1a624f38b9
commit aced4ecfef
2 changed files with 26 additions and 29 deletions

View File

@ -39,6 +39,7 @@ services:
- CLEANUP_INTERVAL_SECONDS=600 - CLEANUP_INTERVAL_SECONDS=600
- POST_LIMIT_WINDOW_SECONDS=86400 - POST_LIMIT_WINDOW_SECONDS=86400
- POST_LIMIT=50 - POST_LIMIT=50
- NODE_ENV=production
depends_on: depends_on:
migrate: migrate:
condition: service_completed_successfully condition: service_completed_successfully

View File

@ -45,42 +45,38 @@ app.listen(process.env.PORT, () => {
app.post( app.post(
"/api/note/", "/api/note/",
postLimiter, postLimiter,
async (req: Request<{}, {}, EncryptedNote>, res) => { (req: Request<{}, {}, EncryptedNote>, res, next) => {
const note = req.body; const note = req.body;
const savedNote = await prisma.encryptedNote.create({ prisma.encryptedNote
data: { ...note, expire_time: addDays(new Date(), 30) }, .create({
}); data: { ...note, expire_time: addDays(new Date(), 30) },
res.json({ })
view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}`, .then((savedNote) => {
expire_time: savedNote.expire_time, res.json({
}); view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}`,
expire_time: savedNote.expire_time,
});
})
.catch(next);
} }
); );
// Get encrypted note // Get encrypted note
app.get("/api/note/:id", async (req, res) => { app.get("/api/note/:id", (req, res, next) => {
const note = await prisma.encryptedNote.findUnique({ prisma.encryptedNote
where: { id: req.params.id }, .findUnique({
}); where: { id: req.params.id },
if (note != null) { })
res.send(note); .then((note) => {
console.log(`[GET] Retrieved note <${note.id}> for <${req.ip}>`); if (note != null) {
} res.send(note);
res.status(404).send(); console.log(`[GET] Retrieved note <${note.id}> for <${req.ip}>`);
}
res.status(404).send();
})
.catch(next);
}); });
// Default response for any other request
app.use((req, res, next) => {
console.log(`Route not found: ${req.path}`);
res.status(404).send("Route not found");
});
// // Error handling
// app.use((err, req, res, next) => {
// console.error(err.stack);
// res.status(500).send("Something broke!");
// });
// Clean up expired notes periodically // Clean up expired notes periodically
const interval = const interval =
Math.max(parseInt(<string>process.env.CLEANUP_INTERVAL_SECONDS) || 1, 1) * Math.max(parseInt(<string>process.env.CLEANUP_INTERVAL_SECONDS) || 1, 1) *