Wrap exception handling around all of loadSession

The user might (probably does) have a session even if we haven't actually tried
to load it yet, so wrap the whole loadSession code in the error handler we were
using for restoring sessions so we gracefully handle exceptions that happen
before trying to restore sessions too.

Remove the catch in MatrixChat that sent you to the login screen.  This is
never the right way to handle an error condition: we should only display the
login screen if we successfully determined that the user has no session, or
they explicitly chose to blow their sessions away.
This commit is contained in:
David Baker 2018-04-27 11:25:13 +01:00
parent db1401f484
commit 0323f8ed0c
3 changed files with 67 additions and 62 deletions

View file

@ -1,6 +1,7 @@
/* /*
Copyright 2015, 2016 OpenMarket Ltd Copyright 2015, 2016 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -65,32 +66,33 @@ import sdk from './index';
* failed. * failed.
*/ */
export function loadSession(opts) { export function loadSession(opts) {
const fragmentQueryParams = opts.fragmentQueryParams || {}; return new Promise.resolve().then(() => {
let enableGuest = opts.enableGuest || false; const fragmentQueryParams = opts.fragmentQueryParams || {};
const guestHsUrl = opts.guestHsUrl; let enableGuest = opts.enableGuest || false;
const guestIsUrl = opts.guestIsUrl; const guestHsUrl = opts.guestHsUrl;
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName; const guestIsUrl = opts.guestIsUrl;
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
if (!guestHsUrl) { if (!guestHsUrl) {
console.warn("Cannot enable guest access: can't determine HS URL to use"); console.warn("Cannot enable guest access: can't determine HS URL to use");
enableGuest = false; enableGuest = false;
} }
if (enableGuest && if (enableGuest &&
fragmentQueryParams.guest_user_id && fragmentQueryParams.guest_user_id &&
fragmentQueryParams.guest_access_token fragmentQueryParams.guest_access_token
) { ) {
console.log("Using guest access credentials"); console.log("Using guest access credentials");
return _doSetLoggedIn({ return _doSetLoggedIn({
userId: fragmentQueryParams.guest_user_id, userId: fragmentQueryParams.guest_user_id,
accessToken: fragmentQueryParams.guest_access_token, accessToken: fragmentQueryParams.guest_access_token,
homeserverUrl: guestHsUrl, homeserverUrl: guestHsUrl,
identityServerUrl: guestIsUrl, identityServerUrl: guestIsUrl,
guest: true, guest: true,
}, true).then(() => true); }, true).then(() => true);
} }
return _restoreFromLocalStorage();
return _restoreFromLocalStorage().then((success) => { }).then((success) => {
if (success) { if (success) {
return true; return true;
} }
@ -101,6 +103,8 @@ export function loadSession(opts) {
// fall back to login screen // fall back to login screen
return false; return false;
}).catch((e) => {
return _handleLoadSessionFailure(e);
}); });
} }
@ -196,43 +200,43 @@ function _registerAsGuest(hsUrl, isUrl, defaultDeviceDisplayName) {
// SessionStore to avoid bugs where the view becomes out-of-sync with // SessionStore to avoid bugs where the view becomes out-of-sync with
// localStorage (e.g. teamToken, isGuest etc.) // localStorage (e.g. teamToken, isGuest etc.)
function _restoreFromLocalStorage() { function _restoreFromLocalStorage() {
if (!localStorage) { return Promise.resolve().then(() => {
return Promise.resolve(false); if (!localStorage) {
} return Promise.resolve(false);
const hsUrl = localStorage.getItem("mx_hs_url"); }
const isUrl = localStorage.getItem("mx_is_url") || 'https://matrix.org'; const hsUrl = localStorage.getItem("mx_hs_url");
const accessToken = localStorage.getItem("mx_access_token"); const isUrl = localStorage.getItem("mx_is_url") || 'https://matrix.org';
const userId = localStorage.getItem("mx_user_id"); const accessToken = localStorage.getItem("mx_access_token");
const deviceId = localStorage.getItem("mx_device_id"); const userId = localStorage.getItem("mx_user_id");
const deviceId = localStorage.getItem("mx_device_id");
let isGuest; let isGuest;
if (localStorage.getItem("mx_is_guest") !== null) { if (localStorage.getItem("mx_is_guest") !== null) {
isGuest = localStorage.getItem("mx_is_guest") === "true"; isGuest = localStorage.getItem("mx_is_guest") === "true";
} else { } else {
// legacy key name // legacy key name
isGuest = localStorage.getItem("matrix-is-guest") === "true"; isGuest = localStorage.getItem("matrix-is-guest") === "true";
} }
if (accessToken && userId && hsUrl) { if (accessToken && userId && hsUrl) {
console.log(`Restoring session for ${userId}`); console.log(`Restoring session for ${userId}`);
return _doSetLoggedIn({ return _doSetLoggedIn({
userId: userId, userId: userId,
deviceId: deviceId, deviceId: deviceId,
accessToken: accessToken, accessToken: accessToken,
homeserverUrl: hsUrl, homeserverUrl: hsUrl,
identityServerUrl: isUrl, identityServerUrl: isUrl,
guest: isGuest, guest: isGuest,
}, false).catch((e) => { }, false).then(() => true);
return _handleRestoreFailure(e); } else {
}).then(() => true); console.log("No previous session found.");
} else { return Promise.resolve(false);
console.log("No previous session found."); }
return Promise.resolve(false); });
}
} }
function _handleRestoreFailure(e) { function _handleLoadSessionFailure(e) {
console.log("Unable to restore session", e); console.log("Unable to load session", e);
const def = Promise.defer(); const def = Promise.defer();
const SessionRestoreErrorDialog = const SessionRestoreErrorDialog =
@ -253,7 +257,7 @@ function _handleRestoreFailure(e) {
} }
// try, try again // try, try again
return _restoreFromLocalStorage(); return loadSession();
}); });
} }

View file

@ -1,7 +1,7 @@
/* /*
Copyright 2015, 2016 OpenMarket Ltd Copyright 2015, 2016 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd Copyright 2017 Vector Creations Ltd
Copyright 2017 New Vector Ltd Copyright 2017, 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -351,15 +351,15 @@ export default React.createClass({
guestIsUrl: this.getCurrentIsUrl(), guestIsUrl: this.getCurrentIsUrl(),
defaultDeviceDisplayName: this.props.defaultDeviceDisplayName, defaultDeviceDisplayName: this.props.defaultDeviceDisplayName,
}); });
}).catch((e) => {
console.error('Error attempting to load session', e);
return false;
}).then((loadedSession) => { }).then((loadedSession) => {
if (!loadedSession) { if (!loadedSession) {
// fall back to showing the login screen // fall back to showing the login screen
dis.dispatch({action: "start_login"}); dis.dispatch({action: "start_login"});
} }
}); });
// Note we don't catch errors from this: we catch everything within
// loadSession as there's logic there to ask the user if they want
// to try logging out.
}).done(); }).done();
}, },

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2017 Vector Creations Ltd Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.