Skip to content

Commit e960dc4

Browse files
authored
Even better error handling in app_page.js (#285)
1 parent b6bfc64 commit e960dc4

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

‎assets/js/app_page.js‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@
1111
// Set when the JSON itself could not be loaded, so the handler below can
1212
// tell a missing data file apart from a failure while building the page.
1313
var loadFailed = false;
14+
// True only once the server has actually answered, so a 404 can be told
15+
// apart from a request that never got that far.
16+
var gotResponse = false;
1417

1518
fetch( '/app/' + encodeURIComponent(name) + '.json')
19+
.catch(function(err) {
20+
// The request never produced a response: DNS, connection reset, CORS,
21+
// offline, or the server closing the socket. There is no status code to
22+
// report, so mark it here rather than in the !r.ok branch below.
23+
loadFailed = true;
24+
throw err;
25+
})
1626
.then(function(r) {
27+
gotResponse = true;
1728
if (!r.ok) {
1829
var httpError = new Error('HTTP ' + r.status + ' ' + r.statusText);
1930
httpError.appName = name;
@@ -187,9 +198,18 @@
187198
console.error('app_page: failed to build app page for "' + name + '"', err);
188199

189200
if (loadFailed) {
190-
var status = err && err.message ? err.message : 'request failed';
191-
root.innerHTML = '<div class="error-box"><h2>App data not found</h2>' +
192-
'<p>Could not load <strong>' + escapeHtml(name) + '</strong>.json (' + escapeHtml(status) + ')</p></div>';
201+
var reason = err && err.message ? err.message : 'request failed';
202+
// A missing file and an unreachable server look identical from here
203+
// without the status, so only claim the file is missing when the
204+
// server actually answered.
205+
if (gotResponse) {
206+
root.innerHTML = '<div class="error-box"><h2>App data not found</h2>' +
207+
'<p>The server responded ' + escapeHtml(reason) + ' for <strong>' + escapeHtml(name) + '</strong>.json.</p></div>';
208+
} else {
209+
root.innerHTML = '<div class="error-box"><h2>Could not reach the server</h2>' +
210+
'<p>The request for <strong>' + escapeHtml(name) + '</strong>.json failed before a response arrived. ' +
211+
'The file may well exist; the connection is what failed.</p></div>';
212+
}
193213
return;
194214
}
195215

0 commit comments

Comments
 (0)