From 7a642ae115139bafd92a3b692d987c78e477c6b0 Mon Sep 17 00:00:00 2001 From: Gospel Amanze Date: Sat, 11 Mar 2023 00:02:08 +0100 Subject: [PATCH 1/3] Added a module that handles async catch errors to avoid using the try catch block --- server/src/controller/item.controller.js | 61 +++++++++--------------- server/src/utils/appError.js | 12 +++++ server/src/utils/catchAsync.js | 6 +++ 3 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 server/src/utils/appError.js create mode 100644 server/src/utils/catchAsync.js diff --git a/server/src/controller/item.controller.js b/server/src/controller/item.controller.js index e2916cf..369eabe 100644 --- a/server/src/controller/item.controller.js +++ b/server/src/controller/item.controller.js @@ -12,6 +12,8 @@ import cloudinary from "cloudinary"; import cloudinaryStorage from "multer-storage-cloudinary"; import config from "../config"; import VerifyToken from "../_helper/VerifyToken"; +import AppError from "../utils/appError"; +import catchAsync from "../utils/catchAsync"; const router = express.Router(); router.use(bodyParser.urlencoded({ extended: true })); @@ -62,60 +64,41 @@ router.post( "/", VerifyToken, cloudImageUpload.single("file"), - async (req, res) => { - try { + catchAsync(async (req, res, next) => { const item = await createItem(req); + if(!item) { + return next(new AppError("There was a problem adding the information to the database.", 500)); + } res.status(201).send(item); - } catch (err) { - return res - .status(500) - .send("There was a problem adding the information to the database."); - } - } + }) ); // GETS A SINGLE ITEM FROM THE DATABASE -router.get("/:id", async (req, res) => { - try { +router.get("/:id", catchAsync(async (req, res, next) => { const item = await getItem(req.params.id); - if (!item) return res.status(404).send("No item found."); + if (!item) return next(new AppError('No item found', 404)); res.status(200).send(item); - } catch (err) { - console.log(err); - return res.status(500).send("There was a problem finding the item."); - } -}); +})); // RETURNS ALL THE ITEMS IN THE DATABASE -router.get("/", async (req, res) => { - try { +router.get("/", catchAsync(async (req, res, next) => { const items = await getItem(); - if (!items) return res.status(404).send("No items found."); + if (!items) return next(new AppError('No items found', 404)); res.status(200).send(items); - } catch (err) { - console.log(err); - return res.status(500).send("There was a problem finding the items."); - } -}); +})); -router.delete("/:id", VerifyToken, async (req, res) => { - try { - await deleteItem(req.params.id); - return res.status(200).send("Iteme: " + item.name + " was deleted."); - } catch (err) { - return res.status(500).send("There was a problem deleting the item."); - } -}); +router.delete("/:id", VerifyToken, catchAsync(async (req, res, next) => { + const item = await deleteItem(req.params.id); + if(!item) return next(new AppError("There was a problem deleting the item.", 404)) + res.status(204).send(`Item: ${item.name} was deleted`); // changed to template literal and corrected a typo +})); // UPDATES A SINGLE ITEM IN THE DATABASE -router.put("/:id", VerifyToken, async (req, res) => { +router.put("/:id", VerifyToken, catchAsync(async (req, res) => { var conditions = { _id: req.params.id, owner: req.userId }; - try { - await updateItem(conditions); + const updateItem = await updateItem(conditions); + if(!updateItem) return next(new AppError('No item with this ID found', 404)); res.status(200).send(item); - } catch (err) { - return res.status(500).send("There was a problem updating the item."); - } -}); +})); export default router; diff --git a/server/src/utils/appError.js b/server/src/utils/appError.js new file mode 100644 index 0000000..2191048 --- /dev/null +++ b/server/src/utils/appError.js @@ -0,0 +1,12 @@ +//Instances of this class will +class AppError extends Error { + constructor(message, statusCode) { + super(message); + this.statusCode = statusCode; + this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error'; + this.isOperational = true; + Error.captureStackTrace(this, this.constructor); + } +} + +module.exports = AppError; \ No newline at end of file diff --git a/server/src/utils/catchAsync.js b/server/src/utils/catchAsync.js new file mode 100644 index 0000000..186a8ff --- /dev/null +++ b/server/src/utils/catchAsync.js @@ -0,0 +1,6 @@ +// THIS FUNCTION CATCHES ERRORS IN ASYNC FUNCTIONS +module.exports = fn => { + return (req, res, next) => { + fn(req, res, next).catch(next); + } +} \ No newline at end of file From 55c2c40a53f3dd0e25a6f2818498b9d25e258bd1 Mon Sep 17 00:00:00 2001 From: Gospel Amanze Date: Mon, 13 Mar 2023 22:12:46 +0100 Subject: [PATCH 2/3] Revert "Added a module that handles async catch errors to avoid using the try catch block" This reverts commit 7a642ae115139bafd92a3b692d987c78e477c6b0. --- server/src/controller/item.controller.js | 61 +++++++++++++++--------- server/src/utils/appError.js | 12 ----- server/src/utils/catchAsync.js | 6 --- 3 files changed, 39 insertions(+), 40 deletions(-) delete mode 100644 server/src/utils/appError.js delete mode 100644 server/src/utils/catchAsync.js diff --git a/server/src/controller/item.controller.js b/server/src/controller/item.controller.js index 369eabe..e2916cf 100644 --- a/server/src/controller/item.controller.js +++ b/server/src/controller/item.controller.js @@ -12,8 +12,6 @@ import cloudinary from "cloudinary"; import cloudinaryStorage from "multer-storage-cloudinary"; import config from "../config"; import VerifyToken from "../_helper/VerifyToken"; -import AppError from "../utils/appError"; -import catchAsync from "../utils/catchAsync"; const router = express.Router(); router.use(bodyParser.urlencoded({ extended: true })); @@ -64,41 +62,60 @@ router.post( "/", VerifyToken, cloudImageUpload.single("file"), - catchAsync(async (req, res, next) => { + async (req, res) => { + try { const item = await createItem(req); - if(!item) { - return next(new AppError("There was a problem adding the information to the database.", 500)); - } res.status(201).send(item); - }) + } catch (err) { + return res + .status(500) + .send("There was a problem adding the information to the database."); + } + } ); // GETS A SINGLE ITEM FROM THE DATABASE -router.get("/:id", catchAsync(async (req, res, next) => { +router.get("/:id", async (req, res) => { + try { const item = await getItem(req.params.id); - if (!item) return next(new AppError('No item found', 404)); + if (!item) return res.status(404).send("No item found."); res.status(200).send(item); -})); + } catch (err) { + console.log(err); + return res.status(500).send("There was a problem finding the item."); + } +}); // RETURNS ALL THE ITEMS IN THE DATABASE -router.get("/", catchAsync(async (req, res, next) => { +router.get("/", async (req, res) => { + try { const items = await getItem(); - if (!items) return next(new AppError('No items found', 404)); + if (!items) return res.status(404).send("No items found."); res.status(200).send(items); -})); + } catch (err) { + console.log(err); + return res.status(500).send("There was a problem finding the items."); + } +}); -router.delete("/:id", VerifyToken, catchAsync(async (req, res, next) => { - const item = await deleteItem(req.params.id); - if(!item) return next(new AppError("There was a problem deleting the item.", 404)) - res.status(204).send(`Item: ${item.name} was deleted`); // changed to template literal and corrected a typo -})); +router.delete("/:id", VerifyToken, async (req, res) => { + try { + await deleteItem(req.params.id); + return res.status(200).send("Iteme: " + item.name + " was deleted."); + } catch (err) { + return res.status(500).send("There was a problem deleting the item."); + } +}); // UPDATES A SINGLE ITEM IN THE DATABASE -router.put("/:id", VerifyToken, catchAsync(async (req, res) => { +router.put("/:id", VerifyToken, async (req, res) => { var conditions = { _id: req.params.id, owner: req.userId }; - const updateItem = await updateItem(conditions); - if(!updateItem) return next(new AppError('No item with this ID found', 404)); + try { + await updateItem(conditions); res.status(200).send(item); -})); + } catch (err) { + return res.status(500).send("There was a problem updating the item."); + } +}); export default router; diff --git a/server/src/utils/appError.js b/server/src/utils/appError.js deleted file mode 100644 index 2191048..0000000 --- a/server/src/utils/appError.js +++ /dev/null @@ -1,12 +0,0 @@ -//Instances of this class will -class AppError extends Error { - constructor(message, statusCode) { - super(message); - this.statusCode = statusCode; - this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error'; - this.isOperational = true; - Error.captureStackTrace(this, this.constructor); - } -} - -module.exports = AppError; \ No newline at end of file diff --git a/server/src/utils/catchAsync.js b/server/src/utils/catchAsync.js deleted file mode 100644 index 186a8ff..0000000 --- a/server/src/utils/catchAsync.js +++ /dev/null @@ -1,6 +0,0 @@ -// THIS FUNCTION CATCHES ERRORS IN ASYNC FUNCTIONS -module.exports = fn => { - return (req, res, next) => { - fn(req, res, next).catch(next); - } -} \ No newline at end of file From faee752b0a8fb7ecd692dd3495b72f5ea0c2e350 Mon Sep 17 00:00:00 2001 From: Gospel Amanze Date: Mon, 13 Mar 2023 22:47:30 +0100 Subject: [PATCH 3/3] added a way to handle async errors with using the try catch block --- server/src/controller/item.controller.js | 8 ++++++++ server/src/utils/appError.js | 13 +++++++++++++ server/src/utils/catchAsync.js | 7 +++++++ 3 files changed, 28 insertions(+) create mode 100644 server/src/utils/appError.js create mode 100644 server/src/utils/catchAsync.js diff --git a/server/src/controller/item.controller.js b/server/src/controller/item.controller.js index e2916cf..f34ec64 100644 --- a/server/src/controller/item.controller.js +++ b/server/src/controller/item.controller.js @@ -12,6 +12,8 @@ import cloudinary from "cloudinary"; import cloudinaryStorage from "multer-storage-cloudinary"; import config from "../config"; import VerifyToken from "../_helper/VerifyToken"; +// import AppError from "../utils/appError"; +// import catchAsync from "../utils/catchAsync"; const router = express.Router(); router.use(bodyParser.urlencoded({ extended: true })); @@ -86,6 +88,12 @@ router.get("/:id", async (req, res) => { } }); +// router.get("/:id", catchAsync(async (req, res) => { +// const item = await getItem(req.params.id); +// if (!item) return next(new AppError('No item found', 404)); +// res.status(200).send(item); +// })); + // RETURNS ALL THE ITEMS IN THE DATABASE router.get("/", async (req, res) => { try { diff --git a/server/src/utils/appError.js b/server/src/utils/appError.js new file mode 100644 index 0000000..303ae06 --- /dev/null +++ b/server/src/utils/appError.js @@ -0,0 +1,13 @@ +class AppError extends Error { + constructor(message, statusCode) { + super(message); + this.statusCode = statusCode; + + this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error'; + this.isOperational = true; + + Error.captureStackTrace(this, this.constructor); + } +} + +module.exports = AppError; \ No newline at end of file diff --git a/server/src/utils/catchAsync.js b/server/src/utils/catchAsync.js new file mode 100644 index 0000000..b3be97b --- /dev/null +++ b/server/src/utils/catchAsync.js @@ -0,0 +1,7 @@ +//Catching error in async functions to avoid using the try catch block everywhere +module.exports = fn => { + return (req, res, next) => { + fn(req, res, next).catch(next); + } +} +