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
- POST_LIMIT_WINDOW_SECONDS=86400
- POST_LIMIT=50
- NODE_ENV=production
depends_on:
migrate:
condition: service_completed_successfully

View File

@ -45,42 +45,38 @@ app.listen(process.env.PORT, () => {
app.post(
"/api/note/",
postLimiter,
async (req: Request<{}, {}, EncryptedNote>, res) => {
(req: Request<{}, {}, EncryptedNote>, res, next) => {
const note = req.body;
const savedNote = await prisma.encryptedNote.create({
data: { ...note, expire_time: addDays(new Date(), 30) },
});
res.json({
view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}`,
expire_time: savedNote.expire_time,
});
prisma.encryptedNote
.create({
data: { ...note, expire_time: addDays(new Date(), 30) },
})
.then((savedNote) => {
res.json({
view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}`,
expire_time: savedNote.expire_time,
});
})
.catch(next);
}
);
// Get encrypted note
app.get("/api/note/:id", async (req, res) => {
const note = await prisma.encryptedNote.findUnique({
where: { id: req.params.id },
});
if (note != null) {
res.send(note);
console.log(`[GET] Retrieved note <${note.id}> for <${req.ip}>`);
}
res.status(404).send();
app.get("/api/note/:id", (req, res, next) => {
prisma.encryptedNote
.findUnique({
where: { id: req.params.id },
})
.then((note) => {
if (note != null) {
res.send(note);
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
const interval =
Math.max(parseInt(<string>process.env.CLEANUP_INTERVAL_SECONDS) || 1, 1) *