Skip to content

Commit 3c245a7

Browse files
authored
Warn when rate limit is on docs page (#4330)
2 parents 2fc37ca + 1b1a3f3 commit 3c245a7

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

docs/index.html

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,22 @@
115115
if (this.version !== this.oldVersion) {
116116
const ConfigurationMdUrl =
117117
`https://raw.githubusercontent.com/rust-lang/rustfmt/${this.version}/Configurations.md`;
118+
let res;
118119
try {
119-
const res = await axios.get(ConfigurationMdUrl);
120-
const {
121-
about,
122-
configurationAbout,
123-
configurationDescriptions
124-
} = parseMarkdownAst(res.data);
125-
this.aboutHtml = marked.parser(about);
126-
this.configurationAboutHtml = marked.parser(configurationAbout);
127-
this.configurationDescriptions = configurationDescriptions;
128-
this.oldVersion = this.version;
129-
} catch(error) {
130-
this.aboutHtml = "<p>Failed to get configuration options for this version, please select the version from the dropdown above.</p>";
120+
res = await axios.get(ConfigurationMdUrl).catch(e => { throw e });
121+
} catch(e) {
122+
this.handleReqFailure(e);
123+
return;
131124
}
125+
const {
126+
about,
127+
configurationAbout,
128+
configurationDescriptions
129+
} = parseMarkdownAst(res.data);
130+
this.aboutHtml = marked.parser(about);
131+
this.configurationAboutHtml = marked.parser(configurationAbout);
132+
this.configurationDescriptions = configurationDescriptions;
133+
this.oldVersion = this.version;
132134
}
133135

134136
const ast = this.configurationDescriptions
@@ -171,7 +173,13 @@
171173
}
172174
},
173175
created: async function() {
174-
const {data: tags} = await axios.get(RusfmtTagsUrl);
176+
let tags;
177+
try {
178+
tags = (await axios.get(RusfmtTagsUrl)).data;
179+
} catch(e) {
180+
this.handleReqFailure(e);
181+
return;
182+
}
175183
const reMajorVersion = /v(\d+)/;
176184
const tagOptions = tags
177185
.map(tag => tag.name)
@@ -184,6 +192,30 @@
184192
if (target != null) {
185193
target.scrollIntoView(true);
186194
}
195+
},
196+
methods: {
197+
handleReqFailure(e) {
198+
if (e.response.status === 404) {
199+
this.aboutHtml =
200+
"<p>Failed to get configuration options for this version, please select the version from the dropdown above.</p>";
201+
} else if (
202+
e.response.status === 403 &&
203+
e.response.headers["X-RateLimit-Remaining"] === 0
204+
) {
205+
const resetDate = new Date(
206+
e.response.headers['X-RateLimit-Reset'] * 1000
207+
).toLocaleString();
208+
this.aboutHtml =
209+
`<p>You have hit the GitHub API rate limit; documentation cannot be updated.` +
210+
`<p>The rate limit will be reset at ${resetDate}.</p>`;
211+
} else {
212+
this.aboutHtml =
213+
`<p>Ecountered an error when fetching documentation data:</p>` +
214+
`<pre><code>${e.response.data}</code></pre>` +
215+
`<p>We would appreciate <a href="https://github.com/rust-lang/rustfmt/issues/new?template=bug_report.md">a bug report</a>.` +
216+
`<p>Try refreshing the page.</p>`;
217+
}
218+
}
187219
}
188220
});
189221
const extractDepthOnes = (ast) => {

0 commit comments

Comments
 (0)