请求修复AdminDirectory日历资源插入报错(Entity already exist)及添加错误处理
Hey there! Let's tackle your two issues: fixing that "Entity already exist" error and adding proper error handling to your calendar resource insertion code.
First, let's unpack why you're seeing that error:
"API call to directory.resources.calendars.insert failed with error Entity already exist"
This means the calendar resource you're trying to add (either via its resourceId or resourceName) is already present in your Google Workspace tenant. The Admin Directory API blocks duplicate resources to avoid conflicts.
Also, there's a critical bug in your current code: you're setting the res variable inside the loop, but the insert call sits outside of it. That means you're only ever trying to insert the last item in your dataset—not every resource you intended to add. Let's fix that first.
Here's the revised code that addresses both the duplicate error and adds robust error handling:
var cus = 'my_customer'; // Loop through each dataset entry (switched to let for block-scoped i) for (let i = 1; i < dataset.length; i++) { const res = { resourceName : dataset[i][0], resourceId : dataset[i][1], resourceType : dataset[i][2] }; try { // First, check if the resource already exists by fetching it AdminDirectory.Resources.Calendars.get(cus, res.resourceId); // If we reach this line, the resource exists—we can update it instead console.log(`Resource ${res.resourceId} already exists. Updating it...`); AdminDirectory.Resources.Calendars.patch(res, cus, res.resourceId); } catch (checkErr) { // If the get call fails, check if it's because the resource doesn't exist if (checkErr.message.includes("Entity not found")) { // Safe to insert the new resource try { AdminDirectory.Resources.Calendars.insert(res, cus); console.log(`Successfully inserted resource ${res.resourceId}`); } catch (insertErr) { // Handle errors that might happen during insertion (e.g., invalid data) console.error(`Failed to insert resource ${res.resourceId}:`, insertErr.message); } } else if (checkErr.message.includes("Entity already exist")) { // Fallback for edge cases where the get call didn't catch the duplicate console.warn(`Resource ${res.resourceId} already exists. Skipping insertion.`); } else { // Handle unexpected errors (e.g., permission issues, API outages) console.error(`Unexpected error checking resource ${res.resourceId}:`, checkErr.message); } } }
In your original code, error handling should be inside the loop, wrapping the insert (and pre-check) operations. Here's why:
- Each resource insertion is independent—an error with one shouldn't stop the rest from processing.
- Using
try-catchblocks lets you target specific errors (like duplicates) and respond appropriately (skip, update, or log) instead of crashing the whole script.
- Duplicate Prevention: We use
Calendars.get()to check for existing resources before inserting, which avoids the "Entity already exist" error entirely. - Update Option: If a resource exists, we use
Calendars.patch()to update it (useCalendars.update()instead if you want to overwrite the entire resource). - Granular Error Handling: Nested
try-catchblocks handle both pre-check errors and insertion-specific errors, so you get clear logs about what went wrong. - Loop Fix: Moved the
insert(and logic) inside the loop so every item in your dataset gets processed.
内容的提问来源于stack exchange,提问作者Manju Parihar

