Skip to content

Commit

Permalink
add example error handling on controller side
Browse files Browse the repository at this point in the history
  • Loading branch information
TONY LIU committed Nov 27, 2024
1 parent 3bb78b9 commit 71af7f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 10 additions & 2 deletions backend/src/routes/logbooks-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 4 additions & 2 deletions backend/src/services/logbooks-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}

Expand All @@ -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}
}
}

0 comments on commit 71af7f8

Please sign in to comment.