diff --git a/errors/customError.js b/errors/customError.js new file mode 100644 index 0000000..3182bc2 --- /dev/null +++ b/errors/customError.js @@ -0,0 +1,9 @@ + +class CustomApiError extends Error { + constructor (message) { + super (message) + } +} + + +module.exports = CustomApiError; \ No newline at end of file diff --git a/middlewares/errorHandler.js b/middlewares/errorHandler.js index ffb3086..ce0f651 100644 --- a/middlewares/errorHandler.js +++ b/middlewares/errorHandler.js @@ -1,7 +1,31 @@ + + const errorHandler = (err,req,res,next) =>{ - return res.status(500).json({ - message:'Internal server error' - }); + let CustomError = { + statusCode :err.statusCode || 500, + msg: err.message || 'something went wrong, Please try again' + } + + // check and handle validation error + if(err.name === 'validation error'){ + CustomError.msg = Object.values(err.errors).map((item) => item.message.join(',')); + CustomError.statusCode = 400; + } + + // check for duplicate values amd handle the error + if(err.code && err.code === 11000){ + CustomError.msg = `Duplicate value entered for ${Object.keys(err.KeyValue)} field, Please chose another value`; + CustomError.statusCode = 400; + } + + // check for cast errors or searchs with wrong id and handle the error + if(err.name === 'CastError'){ + CustomError.msg = `No item with such id`; + CustomError.statusCode = 400; + } + + return res.status(CustomError.statusCode).json({msg: CustomError.msg}); + } diff --git a/models/favourite.js b/models/favourite.js index 6fc1387..2e52b57 100644 --- a/models/favourite.js +++ b/models/favourite.js @@ -13,5 +13,5 @@ const favouriteSchema = new Schema({ }); favouriteSchema.index({ user: 1, favourite: 1 }, { unique: true }); -const favourite = mongoose.model("favourite", favouriteSchema); -module.exports = favourite; \ No newline at end of file +// const favourite = mongoose.model("favourite", favouriteSchema); +module.exports = mongoose.model("favourite", favouriteSchema); \ No newline at end of file diff --git a/models/likes.js b/models/likes.js index 8312fb9..25cbc0d 100644 --- a/models/likes.js +++ b/models/likes.js @@ -13,5 +13,5 @@ const likesSchema = new Schema({ }, { timestamps: true }); likesSchema.index({ user: 1, like: 1 }, { unique: true }); -const ProductLike = mongoose.model("ProductLike", likesSchema); -module.exports = ProductLike; \ No newline at end of file +// const ProductLike = mongoose.model("ProductLike", likesSchema); +module.exports = mongoose.model("ProductLike", likesSchema); \ No newline at end of file diff --git a/models/paystack.js b/models/paystack.js index 000e2ed..3a4039a 100644 --- a/models/paystack.js +++ b/models/paystack.js @@ -27,5 +27,5 @@ const paystackSchema = new Schema({ deliveryLocation: String }) -const Paystack = mongoose.model("Paystack", paystackSchema); -module.exports = Paystack; \ No newline at end of file +// const Paystack = mongoose.model("Paystack", paystackSchema); +module.exports = mongoose.model("Paystack", paystackSchema); // shorter codes like this runs faster. \ No newline at end of file diff --git a/models/user.js b/models/user.js index ee8d4a8..9ebbcdc 100644 --- a/models/user.js +++ b/models/user.js @@ -32,5 +32,5 @@ const userSchema = new Schema({ } }) -const User = mongoose.model("User", userSchema); -module.exports = User; \ No newline at end of file +// const User = mongoose.model("User", userSchema); +module.exports = mongoose.model("User", userSchema); \ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js index 7ed90bb..043b645 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -14,8 +14,6 @@ const { const { upload } = require('../middlewares/cloudinary') const expressBusboy = require('express-busboy') - - router.route('/signup').post(upload.single('picture'), adminSignup) expressBusboy.extend(router).route('/signin').post(adminSignIn) diff --git a/routes/index.js b/routes/index.js index 8228776..be15921 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,8 +1,8 @@ -const express = require("express"); -const router = express.Router(); +// const express = require("express"); +// const router = express.Router(); -router.get("/", (req, res) => { - res.send("Landing page"); -}) +// router.get("/", (req, res) => { +// res.send("Landing page"); +// }) -module.exports = router; \ No newline at end of file +// module.exports = router; \ No newline at end of file diff --git a/server.js b/server.js index e1336d3..ed2695e 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,4 @@ -require('dotenv').config() + require('dotenv').config() const express = require('express') const app = express() const { connectDB } = require('./config/database') @@ -16,7 +16,7 @@ const errorHandler = require('./middlewares/errorHandler') app.use(express.json()) app.use(express.urlencoded({ extended: true })) app.use(bodyParser.urlencoded({ extended: true })) - + app.use(cors()) app.use('/api/v1/', indexRouter) app.use('/api/v1/auth/admin', adminRouter) @@ -28,6 +28,10 @@ app.use('/api/v1/paystack', paystackRouter) app.use(NotFound) app.use(errorHandler) +app.get('/', (req,res) =>{ + res.send('

Shooping site API

Documentation') +}); + const port = process.env.PORT || 3000 const start = async () => { try { diff --git a/utils/catchAsync.js b/utils/catchAsync.js index 9a75555..bc35be5 100644 --- a/utils/catchAsync.js +++ b/utils/catchAsync.js @@ -1,8 +1,3 @@ -// export default (fn) => { -// return (req, res, next) => { -// fn(req, res, next).catch(next); -// }; -// }; const asyncWrapper = (fn)=>{ const x = async(req,res,next) =>{