Skip to content

Commit

Permalink
(chore): Handle too many requests error from vercel
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaKrishnaNamburu committed Sep 21, 2023
1 parent 62ab5fd commit 4777a35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/teleport-publisher-vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export const createVercelPublisher: PublisherFactory<VercelPublisherParams, Verc
if (!vercelAccessToken) {
throw new VercelMissingTokenError()
}

console.info(`[PUBLISHER-VERCEL] Uploading files to Vercel`)
const files = await generateProjectFiles(
projectToPublish,
vercelAccessToken,
Expand All @@ -124,6 +126,7 @@ export const createVercelPublisher: PublisherFactory<VercelPublisherParams, Verc
vercelPayload.alias =
alias.length === 0 && domainAlias ? [`${projectSlug}.${domainAlias}`] : alias

console.info(`[PUBLISHER-VERCEL] Creating deployment`)
const deploymentResult = await createDeployment(vercelPayload, vercelAccessToken, teamId)

return { success: true, payload: deploymentResult }
Expand Down
18 changes: 17 additions & 1 deletion packages/teleport-publisher-vercel/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ export const generateProjectFiles = async (
)

if (!individualUpload) {
console.info(
`[PUBLISHER-VERCEL] - Uploading all the files at once, optes for ${{ individualUpload }}`
)
return projectFilesArray.map((file) => ({
file: file.name,
data: file.content,
encoding: file.contentEncoding,
}))
}

console.info(`[PUBLISHER-VERCEL] - Generating SHA for ${projectFilesArray.length} files`)
const promises = projectFilesArray.map((key) => generateSha(key))
const shaProjectFiles: FileSha[] = await Promise.all(promises)

Expand Down Expand Up @@ -80,7 +84,12 @@ export const generateProjectFiles = async (
if (res.status > 200 && res.status < 500) {
const { error } = (await res.json()) as VercelError

err = new Error(error.message)
/* tslint:disable prefer-conditional-expression */
if (error.code === 'too_many_requests') {
err = new Error(`${error.message} \n ${JSON.stringify(res.headers, null, 2)}`)
} else {
err = new Error(error.message)
}
} else if (res.status !== 200) {
// If something is wrong with the server, we retry
const { error } = (await res.json()) as VercelError
Expand Down Expand Up @@ -305,6 +314,13 @@ export const checkDeploymentStatus = async (deploymentURL: string, teamId?: stri
}

function throwErrorFromVercelResponse(result: VercelError) {
if (result.error.code === 'too_many_requests') {
// https://vercel.com/docs/rest-api#rate-limits
throw new Error(
'You are being rate limited by Vercel. Check the number of files that are being uploaded'
)
}

// https://vercel.com/docs/rest-api#api-basics/errors
// message fields are designed to be neutral,
// not contain sensitive information,
Expand Down

0 comments on commit 4777a35

Please sign in to comment.