Skip to content

Commit 6766436

Browse files
committed
Display a red box of anger for config syntax errors
Fixes element-hq#9519
1 parent 2111db7 commit 6766436

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/i18n/strings/en_EN.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2-
"Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
2+
"Your Riot configuration has invalid JSON in it. Please correct the problem and reload the page. The message from the parser is: %(message)s": "Your Riot configuration has invalid JSON in it. Please correct the problem and reload the page. The message from the parser is: %(message)s",
3+
"Invalid JSON": "Invalid JSON",
34
"Your Riot is misconfigured": "Your Riot is misconfigured",
5+
"Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
46
"Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.": "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.",
57
"Invalid configuration: no default server specified.": "Invalid configuration: no default server specified.",
68
"Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",

src/vector/getconfig.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,32 @@ function getConfig(configJsonFilename) {
3939
request(
4040
{ method: "GET", url: configJsonFilename },
4141
(err, response, body) => {
42-
if (err || response.status < 200 || response.status >= 300) {
43-
// Lack of a config isn't an error, we should
44-
// just use the defaults.
45-
// Also treat a blank config as no config, assuming
46-
// the status code is 0, because we don't get 404s
47-
// from file: URIs so this is the only way we can
48-
// not fail if the file doesn't exist when loading
49-
// from a file:// URI.
50-
if (response) {
51-
if (response.status == 404 || (response.status == 0 && body == '')) {
52-
resolve({});
42+
try {
43+
if (err || response.status < 200 || response.status >= 300) {
44+
// Lack of a config isn't an error, we should
45+
// just use the defaults.
46+
// Also treat a blank config as no config, assuming
47+
// the status code is 0, because we don't get 404s
48+
// from file: URIs so this is the only way we can
49+
// not fail if the file doesn't exist when loading
50+
// from a file:// URI.
51+
if (response) {
52+
if (response.status == 404 || (response.status == 0 && body == '')) {
53+
resolve({});
54+
}
5355
}
56+
reject({err: err, response: response});
57+
return;
5458
}
55-
reject({err: err, response: response});
56-
return;
57-
}
5859

59-
// We parse the JSON ourselves rather than use the JSON
60-
// parameter, since this throws a parse error on empty
61-
// which breaks if there's no config.json and we're
62-
// loading from the filesystem (see above).
63-
resolve(JSON.parse(body));
60+
// We parse the JSON ourselves rather than use the JSON
61+
// parameter, since this throws a parse error on empty
62+
// which breaks if there's no config.json and we're
63+
// loading from the filesystem (see above).
64+
resolve(JSON.parse(body));
65+
} catch (e) {
66+
reject({err: e});
67+
}
6468
},
6569
);
6670
});

src/vector/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,17 @@ async function loadApp() {
222222

223223
let configJson;
224224
let configError;
225+
let configSyntaxError = false;
225226
try {
226227
configJson = await platform.getConfig();
227228
} catch (e) {
228229
configError = e;
230+
231+
if (e && e.err && e.err instanceof SyntaxError) {
232+
console.error("SyntaxError loading config:", e);
233+
configSyntaxError = true;
234+
configJson = {}; // to prevent errors between here and loading CSS for the error box
235+
}
229236
}
230237

231238
// XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure
@@ -295,6 +302,22 @@ async function loadApp() {
295302
}
296303
}
297304

305+
// Now that we've loaded the theme (CSS), display the config syntax error if needed.
306+
if (configSyntaxError) {
307+
const errorMessage = _t(
308+
"Your Riot configuration has invalid JSON in it. Please correct the problem and reload the page. " +
309+
"The message from the parser is: %(message)s",
310+
{message: configError.err.message || _t("Invalid JSON")},
311+
);
312+
313+
const GenericErrorPage = sdk.getComponent("structures.GenericErrorPage");
314+
window.matrixChat = ReactDOM.render(
315+
<GenericErrorPage message={errorMessage} title={_t("Your Riot is misconfigured")} />,
316+
document.getElementById('matrixchat'),
317+
);
318+
return;
319+
}
320+
298321
const validBrowser = checkBrowserFeatures([
299322
"displaytable", "flexbox", "es5object", "es5function", "localstorage",
300323
"objectfit", "indexeddb", "webworkers",

0 commit comments

Comments
 (0)