Skip to content

Commit

Permalink
<PRS1> | async/ await error handling bug | <Murtaza>
Browse files Browse the repository at this point in the history
  • Loading branch information
Murtaza Personal authored and Murtaza Personal committed Jan 10, 2022
1 parent b9506cd commit 18d9093
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 60 deletions.
34 changes: 15 additions & 19 deletions Murtaza/meetings_app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,22 @@ app.use((req, res, next) => {

//Error Handler
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.send({
error: {
status: err.status || 500,
message: err.message,
},
});
res.status(err.status || 500);
res.send({
error: {
status: err.status || 500,
message: err.message,
},
});
});

mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("MongoDB Connected");
app.listen(config.get("app.port"), function () {
console.log(`App listening on ${config.get("app.port")}`);
});
})
.catch((err) => console.log(err));
mongoose.connect(uri, {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => {
console.log("MongoDB Connected");
app.listen(config.get("app.port"), function () {
console.log(`App listening on ${config.get("app.port")}`);
});
})
.catch((err) => console.log(err));

module.exports = app;
40 changes: 20 additions & 20 deletions Murtaza/meetings_app/controller/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ router.use("/meetings", meetingsRouter);
*/
router.get("/", auth, async (req, res, next) => {
userService.findUserByUserId(req.userId).then(result => {
res.status(200).json({ status: 200, data: result.data, message: result.message });
res.status(200).json({ status: 200, data: result.data, message: result.message });
}).catch(err => {
console.log(err);
next(err);
console.log(err);
next(err);
});
});

Expand All @@ -34,14 +34,14 @@ router.get("/", auth, async (req, res, next) => {
router.post("/signup", validateSignUpRequest, async (req, res, next) => {
const userDomainEntity = req.body;
userService.createUserIfNotExists(userDomainEntity)
.then((result) => {
res.cookie("auth-token", result.cookie, { httpOnly: true });
res.status(201).json({data: result.data, message: result.message });
})
.catch((err) => {
console.log(err);
next(err);
});
.then((result) => {
res.cookie("auth-token", result.cookie, { httpOnly: true });
res.status(201).json({data: result.data, message: result.message });
})
.catch((err) => {
console.log(err);
next(err);
});
});


Expand All @@ -51,17 +51,17 @@ router.post("/signup", validateSignUpRequest, async (req, res, next) => {
*
*/
router.post("/login", validateLoginRequest, async (req, res, next) => {
const username = req.body.username;
const username = req.body.username;
const password = req.body.password;
userService.authenticateUser(username, password)
.then((result) => {
res.cookie("auth-token", result.cookie, { httpOnly: true });
res.status(200).json({message: result.message });
})
.catch((err) => {
console.log(err);
next(err);
});
.then((result) => {
res.cookie("auth-token", result.cookie, { httpOnly: true });
res.status(200).json({message: result.message });
})
.catch((err) => {
console.log("somehng");
next(err);
});
});

module.exports = router;
40 changes: 19 additions & 21 deletions Murtaza/meetings_app/controller/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ const validateSignUpRequest = (req, res, next) => {
&& "password" in signUpRequestPayload) {
next();
} else {
throw createError(400, "Mandatory fields are missing");
throw createError(400, "Mandatory fields are missing");
}
};

const validateLoginRequest = async (req, res, next) => {
let loginRequestPayload = req.body;
if ("username" in loginRequestPayload && "password" in loginRequestPayload) {
let user = await UserModel.findOne({ username: loginRequestPayload.username });

if (!user) {
throw createError(400, "Username doesn't exists");
}

next();
} else {
throw createError(400, "username / password field missing");
}
let user = await UserModel.findOne({ username: loginRequestPayload.username });
if (!user) {
next(createError("Username doesn't exists"));
}
next();
} else {
next(createError("username / password field missing"));
}
};

const validateCreateMeetingRequest = async (req, res, next) => {
Expand All @@ -39,17 +37,17 @@ const validateCreateMeetingRequest = async (req, res, next) => {
&& "add" in requestPayload.participants
&& "description" in requestPayload) {
next();
} else {
throw createError(400, "Mandatory fields missing in request body");
}
} else {
next(createError(400, "Mandatory fields missing in request body"));
}
};

const validateFindMeetingRequest = async (req, res, next) => {
let requestParams = req.params;
if (requestParams.meetingId != undefined) {
next();
} else {
throw { status: 400, message: "Invalid Request" };
next(createError(400, "Invalid Request"));
}
};

Expand All @@ -58,15 +56,15 @@ const validateMeetingTimeInfo = (req, res, next) => {
let endTime = req.body.endTime;
if (startTime && endTime) {
if (startTime > endTime) {
throw createError(400, "start date cannot be after the end date");
next(createError(400, "start date cannot be after the end date"));
}

if (startTime < Date.now()) {
throw createError(400, "cannot schedule a meeting in the past" );
next(createError(400, "cannot schedule a meeting in the past" ));
}
next();
} else if (startTime || endTime) {
throw createError(400, "Need to provide both startTime and endTime");
next(createError(400, "Need to provide both startTime and endTime"));
} else {
next();
}
Expand All @@ -90,7 +88,7 @@ const validateAddParticipantRequest = async (req, res, next) => {
}

if (invalidEmailAddresses.length > 0) {
throw createError(400, {data: invalidEmailAddresses, reason: "Some of the provided Email Address doesn't exists in the system"});
next(createError(400, {data: invalidEmailAddresses, reason: "Some of the provided Email Address doesn't exists in the system"}));
}

next();
Expand All @@ -100,12 +98,12 @@ const validateSearchQuery = (req, res, next) => {
if ("description" in req.query || ("from" in req.query && "to" in req.query)) {
if ("from" in req.query && "to" in req.query) {
if (Date.parse(req.query.from) > Date.parse(req.query.to)) {
throw createError(400, "'from' cannot be later in time than 'to'");
next(createError(400, "'from' cannot be later in time than 'to'"));
}
}
next();
} else {
throw createError(400, "please provide either 'description' or 'from/to' timestamps in search query");
next(createError(400, "please provide either 'description' or 'from/to' timestamps in search query"));
}
};

Expand Down

0 comments on commit 18d9093

Please sign in to comment.