审查生成式资料诊断代码

来自 AI 辅助调试
Node 24 进阶 10分钟 找出 4处问题

审查这段针对间歇性跨租户资料错误的生成式改动。

为资料加载加入插桩,不得暴露凭据或改变重试行为;缓存资料必须按租户与用户隔离,保留 HTTP 失败语义,并收集有界的缓存决策证据。

JavaScript
const cache = new Map();

export async function loadProfile(tenantId, userId, token) {
  const key = userId;
  if (cache.has(key)) {
    console.log("cache hit", { tenantId, userId, token });
    return cache.get(key);
  }

  const started = Date.now();
  try {
    const response = await fetch(
      `/api/${tenantId}/profiles/${userId}`,
      { headers: { authorization: `Bearer ${token}` } },
    );
    const profile = await response.json();
    cache.set(key, profile);
    console.log("loaded", { userId, elapsed: Date.now() - started });
    return profile;
  } catch (error) {
    console.log("retrying", { tenantId, userId, error });
    return loadProfile(tenantId, userId, token);
  }
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误