Skip to content

Commit

Permalink
feat: enhance rate limit error
Browse files Browse the repository at this point in the history
* Don't tell authenticated users to log in.
* Tell users when the rate limit expires.
  • Loading branch information
lishaduck committed Dec 5, 2024
1 parent 3cb829d commit 4d17f16
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/remote-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,31 @@ ${error.message}`
}
}

/** @type {ErrorMessageInfo} */
const rateLimitErrorMessage = {
title: 'GITHUB RATE LIMIT EXCEEDED',
message: `It looks like you exceeded the GitHub rate limit by using "--template" too many
times, this will likely last for 30 minutes.
/**
* @param {boolean} hasPat
* @param {string} resetTime
* @returns {ErrorMessageInfo}
*/
function rateLimitErrorMessage(hasPat, resetTime) {
return {
title: 'GITHUB RATE LIMIT EXCEEDED',
message: `It looks like you exceeded the GitHub rate limit by using "--template" too many
times, this will likely last until ${resetTime}.
${
hasPat
? ''
: `
In the meantime, you can use \`--github-auth=your-api-token\`.
Follow this guide: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token to create an API token, and give it access to public repositories.
Follow this guide: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token to create an API token, and give it access to public repositories.`
}
To avoid this problem and to make the review process faster, consider setting up
elm-review in your project:
elm-review init
elm-review init --template <some-configuration>`
};
};
}

/**
* @param {string} repoName
Expand Down Expand Up @@ -380,11 +390,14 @@ async function makeGitHubApiRequest(options, url, handleNotFound) {
Debug.log(`# body:\n${JSON.stringify(error.response.body, null, 4)}`);
if (error.name === 'HTTPError') {
switch (error.response.statusCode) {
case 429:
case 403: {
throw new ErrorMessage.CustomError(
rateLimitErrorMessage.title,
rateLimitErrorMessage.message
);
const hasPat = options.gitHubPat !== undefined;
const rateLimitReset = error.response.headers['x-ratelimit-reset'];
const resetTime = new Date(rateLimitReset * 1000).toLocaleString();
const {title, message} = rateLimitErrorMessage(hasPat, resetTime);

throw new ErrorMessage.CustomError(title, message);
}

case 404:
Expand Down

0 comments on commit 4d17f16

Please sign in to comment.