From aced4ecfef74ef3b21a78455ee4a9bad869ad8ba Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Wed, 6 Jul 2022 15:07:51 +0200 Subject: [PATCH] better error handling --- docker-compose.yml | 1 + server/server.ts | 54 +++++++++++++++++++++------------------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3ca06c9..56dda72 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/server/server.ts b/server/server.ts index 51ec808..ecaa69f 100644 --- a/server/server.ts +++ b/server/server.ts @@ -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(process.env.CLEANUP_INTERVAL_SECONDS) || 1, 1) *