From 71af7f8fbee9a71ad70f6039ec3f216567be81e1 Mon Sep 17 00:00:00 2001 From: TONY LIU Date: Wed, 27 Nov 2024 12:49:00 -0800 Subject: [PATCH] add example error handling on controller side --- backend/src/routes/logbooks-route.js | 12 ++++++++++-- backend/src/services/logbooks-service.js | 6 ++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/src/routes/logbooks-route.js b/backend/src/routes/logbooks-route.js index a846f795..ca8b116c 100644 --- a/backend/src/routes/logbooks-route.js +++ b/backend/src/routes/logbooks-route.js @@ -26,12 +26,20 @@ router.post("/:logbookID/logs", auth, async (req, res) => { router.get("/:logbookID/logs", auth, async (req, res) => { const logbookLogs = await getLogbookLogs(req); - res.status(200).json({ data: logbookLogs }); + if (logbookLogs.error) { + res.status(500).json({ error: logbookLogs.error }); + } else { + res.status(200).json({ data: logbookLogs }); + } }); router.get("/:logbookID/logs/:logID", auth, async (req, res) => { const log = await getLog(req); - res.status(200).json({ data: log }); + if (log.error) { + res.status(500).json({ error: log.error }); + } else { + res.status(200).json({ data: log }); + } }); export default router; \ No newline at end of file diff --git a/backend/src/services/logbooks-service.js b/backend/src/services/logbooks-service.js index cdcba939..ecf9b1c2 100644 --- a/backend/src/services/logbooks-service.js +++ b/backend/src/services/logbooks-service.js @@ -212,7 +212,8 @@ export async function getLogbookLogs(req) { } return data; } catch (error) { - throw new Error(error.message); + console.error(error.message); + return {error: error.message} } } @@ -227,6 +228,7 @@ export async function getLog(req) { } return data[0]; } catch (error) { - throw new Error(error.message); + console.error(error.message); + return {error: error.message} } }