Skip to content

Commit ccbc151

Browse files
committed
fix(trigger): distinguish Drive 403 rate limits from permission errors, preserve knownFileIds on 410 re-seed
1 parent 1ba6339 commit ccbc151

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

apps/sim/lib/webhooks/polling/google-drive.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ export const googleDrivePollingHandler: PollingProviderHandler = {
8989

9090
const config = webhookData.providerConfig as unknown as GoogleDriveWebhookConfig
9191

92-
// First poll: seed page token and known file set
92+
// First poll (or re-seed after 410): seed page token, preserve any existing known file IDs.
9393
if (!config.pageToken) {
9494
const startPageToken = await getStartPageToken(accessToken, config, requestId, logger)
9595
await updateWebhookProviderConfig(
9696
webhookId,
97-
{ pageToken: startPageToken, knownFileIds: [] },
97+
{ pageToken: startPageToken, knownFileIds: config.knownFileIds ?? [] },
9898
logger
9999
)
100100
await markWebhookSuccess(webhookId, logger)
@@ -194,6 +194,16 @@ export const googleDrivePollingHandler: PollingProviderHandler = {
194194
},
195195
}
196196

197+
const DRIVE_RATE_LIMIT_REASONS = new Set(['rateLimitExceeded', 'userRateLimitExceeded'])
198+
199+
/** Returns true only for quota/rate-limit 403s, not permission errors. */
200+
function isDriveRateLimitError(status: number, errorData: Record<string, unknown>): boolean {
201+
if (status !== 403) return false
202+
const reason = (errorData as { error?: { errors?: { reason?: string }[] } })?.error?.errors?.[0]
203+
?.reason
204+
return reason !== undefined && DRIVE_RATE_LIMIT_REASONS.has(reason)
205+
}
206+
197207
async function getStartPageToken(
198208
accessToken: string,
199209
config: GoogleDriveWebhookConfig,
@@ -213,7 +223,7 @@ async function getStartPageToken(
213223
if (!response.ok) {
214224
const status = response.status
215225
const errorData = await response.json().catch(() => ({}))
216-
if (status === 403 || status === 429) {
226+
if (status === 429 || isDriveRateLimitError(status, errorData)) {
217227
const err = new Error(`Drive API rate limit (${status}): ${JSON.stringify(errorData)}`)
218228
err.name = 'DriveRateLimitError'
219229
throw err
@@ -267,7 +277,7 @@ async function fetchChanges(
267277
err.name = 'DrivePageTokenInvalidError'
268278
throw err
269279
}
270-
if (status === 403 || status === 429) {
280+
if (status === 429 || isDriveRateLimitError(status, errorData)) {
271281
const err = new Error(`Drive API rate limit (${status}): ${JSON.stringify(errorData)}`)
272282
err.name = 'DriveRateLimitError'
273283
throw err

0 commit comments

Comments
 (0)