You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修复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.

Breaking Down the Problem

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.

Fixed Code with Error Handling & Duplicate Prevention

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);
    }
  }
}
Where to Add Error Handling

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-catch blocks lets you target specific errors (like duplicates) and respond appropriately (skip, update, or log) instead of crashing the whole script.
Key Improvements
  1. Duplicate Prevention: We use Calendars.get() to check for existing resources before inserting, which avoids the "Entity already exist" error entirely.
  2. Update Option: If a resource exists, we use Calendars.patch() to update it (use Calendars.update() instead if you want to overwrite the entire resource).
  3. Granular Error Handling: Nested try-catch blocks handle both pre-check errors and insertion-specific errors, so you get clear logs about what went wrong.
  4. Loop Fix: Moved the insert (and logic) inside the loop so every item in your dataset gets processed.

内容的提问来源于stack exchange,提问作者Manju Parihar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:32:32