Refactored hardcoded error messages to util file. Added logging to job queue
This commit is contained in:
1 parent
1ad32ed84b
commit
5da22f4403
10 files changed
+138
-60
No files matched your search
@@ -1,9 +1,10 @@
|
||||
const logger = require("../utils/logger");
|
||||
const { errorMessages } = require("../utils/messages");
|
||||
|
||||
const handleErrors = (error, req, res, next) => {
|
||||
const status = error.status || 500;
|
||||
const message = error.message || "Something went wrong";
|
||||
const service = error.service || "Unknown service";
|
||||
const message = error.message || errorMessages.FRIENDLY_ERROR;
|
||||
const service = error.service || errorMessages.UNKNOWN_SERVICE;
|
||||
logger.error(error.message, { service: service });
|
||||
res.status(status).json({ success: false, msg: message });
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ const jwt = require("jsonwebtoken");
|
||||
const logger = require("../utils/logger");
|
||||
const SERVICE_NAME = "verifyJWT";
|
||||
const TOKEN_PREFIX = "Bearer ";
|
||||
|
||||
const { errorMessages } = require("../utils/messages");
|
||||
/**
|
||||
* Verifies the JWT token
|
||||
* @function
|
||||
@@ -15,7 +15,7 @@ const verifyJWT = (req, res, next) => {
|
||||
const token = req.headers["authorization"];
|
||||
// Make sure a token is provided
|
||||
if (!token) {
|
||||
const error = new Error("No token provided");
|
||||
const error = new Error(error.errorMessages.NO_AUTH_TOKEN);
|
||||
error.status = 401;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
@@ -27,8 +27,12 @@ const verifyJWT = (req, res, next) => {
|
||||
// Verify the token's authenticity
|
||||
jwt.verify(parsedToken, process.env.JWT_SECRET, (err, decoded) => {
|
||||
if (err) {
|
||||
logger.error("Invalid token", { service: SERVICE_NAME });
|
||||
return res.status(401).json({ success: false, msg: "Invalid token" });
|
||||
logger.error(errorMessages.INVALID_AUTH_TOKEN, {
|
||||
service: SERVICE_NAME,
|
||||
});
|
||||
return res
|
||||
.status(401)
|
||||
.json({ success: false, msg: errorMessages.INVALID_AUTH_TOKEN });
|
||||
}
|
||||
//Add the user to the request object for use in the route
|
||||
req.user = decoded;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const logger = require("../utils/logger");
|
||||
const SERVICE_NAME = "verifyOwnership";
|
||||
const { errorMessages } = require("../utils/messages");
|
||||
|
||||
const verifyOwnership = (Model, paramName) => {
|
||||
return async (req, res, next) => {
|
||||
@@ -9,10 +10,10 @@ const verifyOwnership = (Model, paramName) => {
|
||||
const doc = await Model.findById(documentId);
|
||||
//If the document is not found, return a 404 error
|
||||
if (!doc) {
|
||||
logger.error("Document not found", {
|
||||
logger.error(errorMessages.VERIFY_OWNER_NOT_FOUND, {
|
||||
service: SERVICE_NAME,
|
||||
});
|
||||
const error = new Error("Document not found");
|
||||
const error = new Error(errorMessages.VERIFY_OWNER_NOT_FOUND);
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
@@ -20,7 +21,7 @@ const verifyOwnership = (Model, paramName) => {
|
||||
// If the userID does not match the document's userID, return a 403 error
|
||||
if (userId.toString() !== doc.userId.toString()) {
|
||||
console.log("boom");
|
||||
const error = new Error("Unauthorized access");
|
||||
const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED);
|
||||
error.status = 403;
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user