Skip to content

Commit

Permalink
SIKKA-4904[closed] updated error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaakher committed Sep 7, 2023
1 parent 72247f5 commit c469850
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sikka/hajar",
"version": "1.0.81",
"version": "1.0.82",
"description": "Toolkit to create SaaS applications",
"author": "Sikka Software <[email protected]> (http://sikka.io)",
"license": "MIT",
Expand Down
75 changes: 59 additions & 16 deletions src/core/authentication/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { default: CustomError } = require("../../utils/customError");

class HajarAuth {
constructor(options) {
this.jwt = options.jwt;
Expand All @@ -13,7 +15,11 @@ class HajarAuth {
// modified
const userExists = await this.User.findOne({ email });
if (userExists) {
throw new Error("User with this email already exists");
throw new CustomError(
"User with this email already exists",
"user-already-exist",
{ email: email }
);
}
const role = await this.Role.find({ name: "Admin" });
const RoleId = role._id;
Expand All @@ -36,7 +42,10 @@ class HajarAuth {
async createRole(roleName, permissionIds) {
const existingRole = await this.Role.findOne({ name: roleName });
if (existingRole) {
throw new Error(`Role ${roleName} already exists`);
// throw new Error(`Role ${roleName} already exists`);
throw new CustomError("Role already exists", "role-already-exist", {
role: roleName,
});
}
let idPermissions = [];
for (let i = 0; i < permissionIds.length; i++) {
Expand Down Expand Up @@ -65,15 +74,23 @@ class HajarAuth {
async signin(email, password, res) {
const user = await this.User.findOne({ email });
if (!user) {
throw new Error("Invalid email or password");
// throw new Error("Invalid email or password");
throw new CustomError(
"Invalid email or password",
"invalid-email-password"
);
}

const isPasswordCorrect = await this.bcrypt.compare(
password,
user.password
);
if (!isPasswordCorrect) {
throw new Error("Invalid email or password");
// throw new Error("Invalid email or password");
throw new CustomError(
"Invalid email or password",
"invalid-email-password"
);
}

const token = this.jwt.sign({ userId: user._id }, this.secret);
Expand All @@ -98,12 +115,14 @@ class HajarAuth {
const user = await this.User.findById(decodedToken.userId);
if (!user) {
console.error(`User not found for ID ${decodedToken.userId}`);
return { error: "User not found" };
// return { error: "User not found" };
return new CustomError("User not found", "user-not-found");
}
return user;
} catch (err) {
console.error("JWT verification error:", err);
return { error: "Invalid token" };
// return { error: "Invalid token" };
return new CustomError("Invalid user token", "invalid-user-token");
}
}

Expand All @@ -116,7 +135,10 @@ class HajarAuth {
},
});
if (!user) {
throw new Error(`User with email ${email} not found`);
// throw new Error(`User with email ${email} not found`);
throw new CustomError("User not found", "user-not-found", {
email: email,
});
}

return user;
Expand All @@ -130,24 +152,36 @@ class HajarAuth {
},
});
if (!user) {
throw new Error(`User with ID ${userId} not found`);
// throw new Error(`User with ID ${userId} not found`);
throw new CustomError("User not found", "user-not-found", {
userID: userId,
});
}

return user;
}

async getRoleById(middelware, roleId) {
if (!middelware.Types.ObjectId.isValid(roleId)) {
throw new Error("Invalid roleId");
// throw new Error("Invalid roleId");
throw new CustomError("Invalid Role ID", "invalid-role", {
roleID: roleId,
});
}
try {
const role = await this.Role.findOne({ _id: roleId });
if (!role) {
throw new Error("Role not found");
// throw new Error("Role not found");
throw new CustomError("Role not found", "role-not-found", {
roleID: roleId,
});
}
return role;
} catch (error) {
throw new Error(`Unable to fetch role: ${error.message}`);
// throw new Error(`Unable to fetch role: ${error.message}`);
throw new CustomError(error.message, "fetch-role-error", {
roleID: roleId,
});
}
}
// Fetch all roles from the database
Expand All @@ -156,7 +190,8 @@ class HajarAuth {
const roles = await this.Role.find();
return roles;
} catch (error) {
throw new Error(`Unable to fetch roles: ${error.message}`);
// throw new Error(`Unable to fetch roles: ${error.message}`);
throw new CustomError(error.message, "fetch-role-error");
}
}

Expand All @@ -165,17 +200,24 @@ class HajarAuth {
try {
const role = await this.Role.findByIdAndRemove(roleId);
if (!role) {
throw new Error("Role not found");
// throw new Error("Role not found");
throw new CustomError("Role not found", "role-not-found", {
roleID: roleId,
});
}
} catch (error) {
throw new Error(`Unable to delete role: ${error.message}`);
// throw new Error(`Unable to delete role: ${error.message}`);
throw new CustomError(error.message, "delete-role-error");
}
}

async updateRole(roleId, name, permissionIds) {
const role = await this.Role.findById(roleId);
if (!role) {
throw new Error("Role not found");
// throw new Error("Role not found");
throw new CustomError("Role not found", "role-not-found", {
roleID: roleId,
});
}
role.name = name;
let idPermissions = [];
Expand Down Expand Up @@ -203,7 +245,8 @@ class HajarAuth {
const grants = await this.Permission.find().distinct("grant");
return [...grants];
} catch (error) {
throw new Error(`Unable to fetch permissions: ${error.message}`);
// throw new Error(`Unable to fetch permissions: ${error.message}`);
throw new CustomError(error.message, "fetch-permission-error");
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils/customError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CustomError extends Error {
constructor(message, slug, customProperties) {
super(message); // Pass the message parameter to the Error constructor
this.slug = slug; // Assign the code parameter to a property on the error object
Object.assign(this, customProperties); // Assign any additional properties to the error object
}
}

export default CustomError;

0 comments on commit c469850

Please sign in to comment.