Skip to content

Commit

Permalink
processObjectId function could be more robust by adding type checking…
Browse files Browse the repository at this point in the history
… and error handling
  • Loading branch information
Baalmart committed Dec 13, 2024
1 parent 73108c1 commit 00a93ae
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/auth-service/models/Preference.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,14 @@ PreferenceSchema.pre(

// Utility function to validate and process ObjectIds
const processObjectId = (id) => {
return id instanceof mongoose.Types.ObjectId
? id
: mongoose.Types.ObjectId(id);
if (!id) return null;
if (id instanceof mongoose.Types.ObjectId) return id;
try {
return mongoose.Types.ObjectId(id);
} catch (error) {
logger.error(`Invalid ObjectId: ${id}`);
throw new Error(`Invalid ObjectId: ${id}`);
}
};

// Comprehensive ID fields processing
Expand Down

0 comments on commit 00a93ae

Please sign in to comment.