Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/app/api/core/utils/withRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { StatusableError } from '@/types/CopilotApiError'
import pRetry, { FailedAttemptError } from 'p-retry'
import * as Sentry from '@sentry/nextjs'

export const RETRY_404_ENABLED = process.env.RETRY_404 === 'true'

export const withRetry = async <T>(fn: (...args: any[]) => Promise<T>, args: any[]): Promise<T> => {
let isEventProcessorRegistered = false

Expand Down Expand Up @@ -41,7 +43,11 @@ export const withRetry = async <T>(fn: (...args: any[]) => Promise<T>, args: any
// Typecasting because Copilot doesn't export an error class
const err = error as StatusableError
// Retry if statusCode is 429 (ratelimit), 408 (timeouts), or any server related (5xx) error
return [408, 429].includes(err.status) || (err.status >= 500 && err.status <= 511)
return (
[408, 429].includes(err.status) ||
(err.status >= 500 && err.status <= 511) ||
(RETRY_404_ENABLED && err.status === 404)
)
},
},
)
Expand Down
Loading