|
11 | 11 | // Set when the JSON itself could not be loaded, so the handler below can |
12 | 12 | // tell a missing data file apart from a failure while building the page. |
13 | 13 | 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; |
14 | 17 |
|
15 | 18 | 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 | + }) |
16 | 26 | .then(function(r) { |
| 27 | + gotResponse = true; |
17 | 28 | if (!r.ok) { |
18 | 29 | var httpError = new Error('HTTP ' + r.status + ' ' + r.statusText); |
19 | 30 | httpError.appName = name; |
|
187 | 198 | console.error('app_page: failed to build app page for "' + name + '"', err); |
188 | 199 |
|
189 | 200 | 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 | + } |
193 | 213 | return; |
194 | 214 | } |
195 | 215 |
|
|
0 commit comments