Skip to content

Commit b574391

Browse files
committed
added logger at all catchers
1 parent 9d4a04e commit b574391

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

controller/auth.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import util from "#util";
1+
import util, {logger} from "#util";
22
import { authenticateUser, userExists, updatePassword } from "#services/user";
33

44
const otpStore = {};
@@ -17,6 +17,7 @@ async function login(req, res) {
1717
userDetails.token = token;
1818
res.json({ res: "welcome", user: userDetails });
1919
} catch (error) {
20+
logger.error("Error while login", error)
2021
if (error.name === "UserDoesNotExist") {
2122
res.status(403);
2223
res.json({ err: "Incorrect ID password" });
@@ -50,6 +51,7 @@ async function resetPassword(req, res) {
5051
await updatePassword(uid, password);
5152
res.json({ res: "successfully updated password" });
5253
} catch (error) {
54+
logger.log("Error while updating", error)
5355
res.status(500);
5456
if (error.name === "UpdateError") res.json({ err: "Something went wrong while updating password" });
5557
else res.json({ err: "something went wrong" });

controller/user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { allUsers, createUser } from "#services/user";
2+
import { logger } from "#util";
23

34
async function addUser(req, res) {
45
const {
@@ -8,6 +9,8 @@ async function addUser(req, res) {
89
const newUser = await createUser(name, password, emailId, uid, userType);
910
res.json({ res: `added user ${newUser.id}` });
1011
} catch (error) {
12+
logger.error("Error while inserting", error)
13+
res.status(500)
1114
res.json({ err: "Error while inserting in DB" });
1215
}
1316
}

models/user.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ async function create(name, password, emailId, uid, userType) {
2525
uid,
2626
userType,
2727
});
28-
const userDoc = await user.save().catch((err) => { logger.error(err); });
28+
const userDoc = await user.save();
2929
return userDoc;
3030
}
3131

3232
async function read(filter, limit = 1) {
33-
const userData = await User.find(filter).limit(limit).catch((err) => { logger.error(err); });
33+
const userData = await User.find(filter).limit(limit);
3434
return userData;
3535
}
3636

3737
async function update(filter, updateObject) {
3838
const user = await User.findOneAndUpdate(filter, updateObject, { new: true });
39-
if (user.id) return user;
40-
return "Error";
39+
return user;
4140
}
4241

4342
export default {

services/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import databaseError from "#error/database";
33

44
export async function authenticateUser(uid, password) {
55
const user = await User.read({ uid, password }, 1);
6-
if (user[0].uid === uid) {
6+
if (user[0]?.uid === uid) {
77
return user[0];
88
}
99
throw new databaseError.UserDoesNotExistError();

0 commit comments

Comments
 (0)