From 0e8dc24c3ff6c78f08b5101b260bdee1cc02b956 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Wed, 2 Oct 2019 16:26:23 +0100 Subject: [PATCH 1/2] Add a basic error boundary for the entire app This adds a basic error boundary around the entire app to catch errors during rendering and present the user with the options on how to proceed. This is not implemented as a modal so that it could be used selectively in portions of the app as well, such as just the `RoomView`. Fixes https://github.com/vector-im/riot-web/issues/11009 --- res/css/_components.scss | 1 + res/css/views/elements/_ErrorBoundary.scss | 34 ++++++ src/components/structures/MatrixChat.js | 62 +++++------ .../views/elements/ErrorBoundary.js | 104 ++++++++++++++++++ .../settings/tabs/user/HelpUserSettingsTab.js | 2 +- src/i18n/strings/en_EN.json | 3 +- 6 files changed, 169 insertions(+), 37 deletions(-) create mode 100644 res/css/views/elements/_ErrorBoundary.scss create mode 100644 src/components/views/elements/ErrorBoundary.js diff --git a/res/css/_components.scss b/res/css/_components.scss index 40a797dc15..f627fe3a29 100644 --- a/res/css/_components.scss +++ b/res/css/_components.scss @@ -87,6 +87,7 @@ @import "./views/elements/_DirectorySearchBox.scss"; @import "./views/elements/_Dropdown.scss"; @import "./views/elements/_EditableItemList.scss"; +@import "./views/elements/_ErrorBoundary.scss"; @import "./views/elements/_Field.scss"; @import "./views/elements/_ImageView.scss"; @import "./views/elements/_InlineSpinner.scss"; diff --git a/res/css/views/elements/_ErrorBoundary.scss b/res/css/views/elements/_ErrorBoundary.scss new file mode 100644 index 0000000000..e46ba69a7c --- /dev/null +++ b/res/css/views/elements/_ErrorBoundary.scss @@ -0,0 +1,34 @@ +/* +Copyright 2019 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +.mx_ErrorBoundary { + width: 100%; + height: 100%; + display: flex; + align-items: center; + justify-content: center; +} + +.mx_ErrorBoundary_body { + display: flex; + flex-direction: column; + max-width: 400px; + align-items: center; + + .mx_AccessibleButton { + margin-top: 5px; + } +} diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 2da219a28d..da67416400 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -1808,28 +1808,26 @@ export default createReactClass({ render: function() { // console.log(`Rendering MatrixChat with view ${this.state.view}`); + let view; + if ( this.state.view === VIEWS.LOADING || this.state.view === VIEWS.LOGGING_IN ) { const Spinner = sdk.getComponent('elements.Spinner'); - return ( + view = (
); - } - - // needs to be before normal PageTypes as you are logged in technically - if (this.state.view === VIEWS.POST_REGISTRATION) { + } else if (this.state.view === VIEWS.POST_REGISTRATION) { + // needs to be before normal PageTypes as you are logged in technically const PostRegistration = sdk.getComponent('structures.auth.PostRegistration'); - return ( + view = ( ); - } - - if (this.state.view === VIEWS.LOGGED_IN) { + } else if (this.state.view === VIEWS.LOGGED_IN) { // store errors stop the client syncing and require user intervention, so we'll // be showing a dialog. Don't show anything else. const isStoreError = this.state.syncError && this.state.syncError instanceof Matrix.InvalidStoreError; @@ -1843,8 +1841,8 @@ export default createReactClass({ * as using something like redux to avoid having a billion bits of state kicking around. */ const LoggedInView = sdk.getComponent('structures.LoggedInView'); - return ( - ; } - return ( + view = (
{errorBox} - { _t('Logout') } + {_t('Logout')}
); } - } - - if (this.state.view === VIEWS.WELCOME) { + } else if (this.state.view === VIEWS.WELCOME) { const Welcome = sdk.getComponent('auth.Welcome'); - return ; - } - - if (this.state.view === VIEWS.REGISTER) { + view = ; + } else if (this.state.view === VIEWS.REGISTER) { const Registration = sdk.getComponent('structures.auth.Registration'); - return ( + view = ( ); - } - - - if (this.state.view === VIEWS.FORGOT_PASSWORD) { + } else if (this.state.view === VIEWS.FORGOT_PASSWORD) { const ForgotPassword = sdk.getComponent('structures.auth.ForgotPassword'); - return ( + view = ( ); - } - - if (this.state.view === VIEWS.LOGIN) { + } else if (this.state.view === VIEWS.LOGIN) { const Login = sdk.getComponent('structures.auth.Login'); - return ( + view = ( ); - } - - if (this.state.view === VIEWS.SOFT_LOGOUT) { + } else if (this.state.view === VIEWS.SOFT_LOGOUT) { const SoftLogout = sdk.getComponent('structures.auth.SoftLogout'); - return ( + view = ( ); + } else { + console.error(`Unknown view ${this.state.view}`); } - console.error(`Unknown view ${this.state.view}`); + const ErrorBoundary = sdk.getComponent('elements.ErrorBoundary'); + return + {view} + ; }, }); diff --git a/src/components/views/elements/ErrorBoundary.js b/src/components/views/elements/ErrorBoundary.js new file mode 100644 index 0000000000..630b369caa --- /dev/null +++ b/src/components/views/elements/ErrorBoundary.js @@ -0,0 +1,104 @@ +/* +Copyright 2019 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import React from 'react'; +import sdk from '../../../index'; +import { _t } from '../../../languageHandler'; +import MatrixClientPeg from '../../../MatrixClientPeg'; +import PlatformPeg from '../../../PlatformPeg'; +import Modal from '../../../Modal'; + +/** + * This error boundary component can be used to wrap large content areas and + * catch exceptions during rendering in the component tree below them. + */ +export default class ErrorBoundary extends React.PureComponent { + constructor(props) { + super(props); + + this.state = { + error: null, + }; + } + + static getDerivedStateFromError(error) { + // Side effects are not permitted here, so we only update the state so + // that the next render shows an error message. + return { error }; + } + + componentDidCatch(error, { componentStack }) { + // Browser consoles are better at formatting output when native errors are passed + // in their own `console.error` invocation. + console.error(error); + console.error( + "The above error occured while React was rendering the following components:", + componentStack, + ); + } + + _onClearCacheAndReload = () => { + if (!PlatformPeg.get()) return; + + MatrixClientPeg.get().stopClient(); + MatrixClientPeg.get().store.deleteAllData().done(() => { + PlatformPeg.get().reload(); + }); + }; + + _onBugReport = () => { + const BugReportDialog = sdk.getComponent("dialogs.BugReportDialog"); + if (!BugReportDialog) { + return; + } + Modal.createTrackedDialog('Bug Report Dialog', '', BugReportDialog, {}); + }; + + render() { + if (this.state.error) { + const AccessibleButton = sdk.getComponent('elements.AccessibleButton'); + const newIssueUrl = "https://github.com/vector-im/riot-web/issues/new"; + return
+
+

{_t("Something went wrong!")}

+

{_t( + "Please create a new issue " + + "on GitHub so that we can investigate this bug.", {}, { + newIssueLink: (sub) => { + return { sub }; + }, + }, + )}

+

{_t( + "If you've submitted a bug via GitHub, debug logs can help " + + "us track down the problem. Debug logs contain application " + + "usage data including your username, the IDs or aliases of " + + "the rooms or groups you have visited and the usernames of " + + "other users. They do not contain messages.", + )}

+ + {_t("Submit debug logs")} + + + {_t("Clear cache and reload")} + +
+
; + } + + return this.props.children; + } +} diff --git a/src/components/views/settings/tabs/user/HelpUserSettingsTab.js b/src/components/views/settings/tabs/user/HelpUserSettingsTab.js index a50b26fed6..ccabee3ca8 100644 --- a/src/components/views/settings/tabs/user/HelpUserSettingsTab.js +++ b/src/components/views/settings/tabs/user/HelpUserSettingsTab.js @@ -226,7 +226,7 @@ export default class HelpUserSettingsTab extends React.Component {
- {_t("Clear Cache and Reload")} + {_t("Clear cache and reload")}
diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 5a683ce92d..4f25c25ec2 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -617,7 +617,7 @@ "Bug reporting": "Bug reporting", "If you've submitted a bug via GitHub, debug logs can help us track down the problem. Debug logs contain application usage data including your username, the IDs or aliases of the rooms or groups you have visited and the usernames of other users. They do not contain messages.": "If you've submitted a bug via GitHub, debug logs can help us track down the problem. Debug logs contain application usage data including your username, the IDs or aliases of the rooms or groups you have visited and the usernames of other users. They do not contain messages.", "Submit debug logs": "Submit debug logs", - "Clear Cache and Reload": "Clear Cache and Reload", + "Clear cache and reload": "Clear cache and reload", "FAQ": "FAQ", "Versions": "Versions", "matrix-react-sdk version:": "matrix-react-sdk version:", @@ -1124,6 +1124,7 @@ "No results": "No results", "Yes": "Yes", "No": "No", + "Please create a new issue on GitHub so that we can investigate this bug.": "Please create a new issue on GitHub so that we can investigate this bug.", "Communities": "Communities", "You cannot delete this image. (%(code)s)": "You cannot delete this image. (%(code)s)", "Uploaded on %(date)s by %(user)s": "Uploaded on %(date)s by %(user)s", From b605c0048d0e44c1f573466764fe778fa7baf2a7 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Wed, 2 Oct 2019 17:28:03 +0100 Subject: [PATCH 2/2] Add an error boundary around the RoomView This adds a more specific boundary around the `RoomView` for room-specific errors and is an example how we could use add boundaries around just a portion of the app. --- src/components/structures/RoomView.js | 135 ++++++++++++++------------ 1 file changed, 73 insertions(+), 62 deletions(-) diff --git a/src/components/structures/RoomView.js b/src/components/structures/RoomView.js index 4d52158dae..13523104ae 100644 --- a/src/components/structures/RoomView.js +++ b/src/components/structures/RoomView.js @@ -1566,20 +1566,23 @@ module.exports = createReactClass({ const TimelinePanel = sdk.getComponent("structures.TimelinePanel"); const RoomUpgradeWarningBar = sdk.getComponent("rooms.RoomUpgradeWarningBar"); const RoomRecoveryReminder = sdk.getComponent("rooms.RoomRecoveryReminder"); + const ErrorBoundary = sdk.getComponent("elements.ErrorBoundary"); if (!this.state.room) { const loading = this.state.roomLoading || this.state.peekLoading; if (loading) { return (
- + + +
); } else { @@ -1597,18 +1600,20 @@ module.exports = createReactClass({ const roomAlias = this.state.roomAlias; return (
- + + +
); } @@ -1618,12 +1623,14 @@ module.exports = createReactClass({ if (myMembership == 'invite') { if (this.state.joining || this.state.rejecting) { return ( - + + ); } else { const myUserId = MatrixClientPeg.get().credentials.userId; @@ -1638,14 +1645,16 @@ module.exports = createReactClass({ // We have a regular invite for this room. return (
- + + +
); } @@ -1942,41 +1951,43 @@ module.exports = createReactClass({ return (
- - -
- { auxPanel } -
- { topUnreadMessagesBar } - { jumpToBottom } - { messagePanel } - { searchResultsPanel } -
-
-
-
- { statusBar } + + + +
+ {auxPanel} +
+ {topUnreadMessagesBar} + {jumpToBottom} + {messagePanel} + {searchResultsPanel}
+
+
+
+ {statusBar} +
+
+ {previewBar} + {messageComposer}
- { previewBar } - { messageComposer } -
- + +
); },