Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions apps/sim/connectors/google-drive/google-drive-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
resolveRetryDelayMs,
retryWithExponentialBackoff,
} from '@/lib/knowledge/documents/utils'
import {
ConnectorSourceError,
type ConnectorSourceFailureCategory,
} from '@/connectors/source-error'
import { readBodyWithLimit } from '@/connectors/utils'

const GOOGLE_ERROR_BODY_MAX_BYTES = 64 * 1024
Expand Down Expand Up @@ -103,22 +107,47 @@ function classifyGoogleDriveError(
return 'unknown'
}

export class GoogleDriveApiError extends Error {
function diagnosticCategory(
kind: GoogleDriveErrorKind,
status: number
): ConnectorSourceFailureCategory | undefined {
switch (kind) {
case 'authorization':
case 'permission':
return 'authorization'
case 'not_found':
return 'source_unavailable'
case 'export_too_large':
case 'unsupported_export':
case 'policy':
return 'request_rejected'
case 'quota':
return 'rate_limit'
case 'transient':
return status === 429 || status === 403 ? 'rate_limit' : 'provider_unavailable'
default:
return undefined
}
}

export class GoogleDriveApiError extends ConnectorSourceError {
retryAfterMs?: number
readonly reasons: readonly string[]
readonly kind: GoogleDriveErrorKind
readonly rateLimited: boolean

constructor(
readonly status: number,
normalizedReasons: readonly string[]
) {
constructor(status: number, normalizedReasons: readonly string[]) {
const diagnosticReasons = normalizedReasons.slice(0, GOOGLE_ERROR_REASON_MAX_COUNT)
const reasonSuffix = diagnosticReasons.length > 0 ? ` (${diagnosticReasons.join(', ')})` : ''
super(`Google Drive API request failed with HTTP ${status}${reasonSuffix}`)
const kind = classifyGoogleDriveError(status, normalizedReasons)
super(
`Google Drive API request failed with HTTP ${status}${reasonSuffix}`,
status,
diagnosticCategory(kind, status)
)
this.name = 'GoogleDriveApiError'
this.reasons = diagnosticReasons
this.kind = classifyGoogleDriveError(status, normalizedReasons)
this.kind = kind
this.rateLimited =
status === 429 || normalizedReasons.some((reason) => RATE_LIMIT_REASONS.has(reason))
}
Expand Down
8 changes: 6 additions & 2 deletions apps/sim/connectors/google-drive/google-drive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,12 @@ describe('Google Drive change feed', () => {
{ kind: 'removed', externalId: 'moved-out' },
{ kind: 'removed', externalId: 'video' },
])
expect(result.nextCursor).toBe('5000')
expect(result.hasMore).toBe(false)
expect(result.nextCursor).toMatch(/^gdrive-shortcuts:v1:/)
expect(result.hasMore).toBe(true)
mockFetch.mockResolvedValueOnce(jsonResponse({ files: [] }))
await expect(
googleDriveConnector.listChanges!('token', {}, result.nextCursor!)
).resolves.toEqual({ changes: [], nextCursor: '5000', hasMore: false })
const url = new URL(String(mockFetch.mock.calls[0][0]))
expect(url.searchParams.get('pageToken')).toBe('4821')
expect(url.searchParams.get('includeRemoved')).toBe('true')
Expand Down
Loading
Loading