Skip to content

Commit

Permalink
refactor: Add throwIfMissing function for error handling in utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelink committed May 28, 2024
1 parent 2560b1f commit 9097997
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Throws an error if any of the keys are missing from the object
* @param {*} obj
* @param {string[]} keys
* @throws {Error}
*/
export function throwIfMissing(obj, keys) {
const missing = [];
for (let key of keys) {
if (!(key in obj) || !obj[key]) {
missing.push(key);
}
}
if (missing.length > 0) {
throw new Error(`Missing required fields: ${missing.join(", ")}`);
}
}

0 comments on commit 9097997

Please sign in to comment.