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); + } +} +