From 4d17f166b418b0dc09638cc643a5106a1cc1f07b Mon Sep 17 00:00:00 2001 From: Eli <88557639+lishaduck@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:17:32 -0600 Subject: [PATCH] feat: enhance rate limit error * Don't tell authenticated users to log in. * Tell users when the rate limit expires. --- lib/remote-template.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/remote-template.js b/lib/remote-template.js index 08eeaa73..2c56efbd 100644 --- a/lib/remote-template.js +++ b/lib/remote-template.js @@ -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 ` -}; + }; +} /** * @param {string} repoName @@ -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: