Custom Error Class
A CLASS IS CREATED THAT EXTENDS THE ERROR CLASS .(IN ANOTHER FILE MyError.js)
class MyError extends Error {
constructor(status, message) {
super();
this.status = status;
this.message = message;
}
}
module.exports = MyError;
IT IS REQUIRED IN APP.JS
const MyError = require("./MyError");
const checkToken = (req, res, next) => {
let { token } = req.query;
if (token === "giveaccess") {
next();
}
// throw new Error("ACCESS DENIED");
throw new MyError(401, "ACCESS DENIED");
};
app.use((err, req, res, next) => {
console.log("-----ERROR------");
res.send(err)
});

Comments
Post a Comment