Skip to content

Commit

Permalink
Rework unexpected error modal to trigger for exceptions blocking rend…
Browse files Browse the repository at this point in the history
…ering
  • Loading branch information
refi93 committed Aug 9, 2022
1 parent de111ec commit 73d6742
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
19 changes: 10 additions & 9 deletions app/frontend/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import NavbarAuth from './common/navbar/navbarAuth'
import NavbarUnauth from './common/navbar/navbarUnauth'
import AutoLogout from './autoLogout'
import {ADALITE_CONFIG} from '../config'
import UnexpectedErrorModal from './common/unexpectedErrorModal'
import Exchange from './pages/exchange/exchange'
import NufiPreviewPage from './pages/nufiPreview/nufiPreviewPage'
import ErrorBoundary from './errorBoundary'

const {ADALITE_LOGOUT_AFTER} = ADALITE_CONFIG

Expand Down Expand Up @@ -39,14 +39,15 @@ const App = connect((state) => ({
}
return (
<div className="wrap">
<LoadingOverlay />
<Navbar />
<TopLevelRouter />
<Footer />
{ADALITE_LOGOUT_AFTER > 0 && <AutoLogout />}
{displayWelcome && <Welcome />}
{shouldShowContactFormModal && <ContactForm />}
{shouldShowUnexpectedErrorModal && <UnexpectedErrorModal />}
<ErrorBoundary>
<LoadingOverlay />
<Navbar />
<TopLevelRouter />
<Footer />
{ADALITE_LOGOUT_AFTER > 0 && <AutoLogout />}
{displayWelcome && <Welcome />}
{shouldShowContactFormModal && <ContactForm />}
</ErrorBoundary>
</div>
)
})
Expand Down
16 changes: 14 additions & 2 deletions app/frontend/components/common/unexpectedErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import actions from '../../actions'
import Modal from './modal'
import Alert from './alert'
import submitFeedbackToSentry from '../../helpers/submitFeedbackToSentry'
import * as Sentry from '@sentry/browser'

interface Props {
sendSentry: any
closeUnexpectedErrorModal: () => void
reloadPageOnClose: boolean
}

const UnexpectedErrorModal = ({sendSentry, closeUnexpectedErrorModal}: Props) => {
const UnexpectedErrorModal = ({
sendSentry,
closeUnexpectedErrorModal,
reloadPageOnClose,
}: Props) => {
const [userEmail, setEmail] = useState('')
const [userName, setName] = useState('')
const [userComments, setComments] = useState('')
Expand All @@ -31,8 +37,14 @@ const UnexpectedErrorModal = ({sendSentry, closeUnexpectedErrorModal}: Props) =>
sendSentry.resolve(false)
}
closeUnexpectedErrorModal()

if (reloadPageOnClose) {
// workaround to allow for sentry to queue up the error to be sent
// before the app is reloaded
setTimeout(() => Sentry.close(5000).then(() => window.location.reload()), 300)
}
},
[userComments, userEmail, userName, sendSentry, closeUnexpectedErrorModal]
[userComments, userEmail, userName, sendSentry, closeUnexpectedErrorModal, reloadPageOnClose]
)
const cancelAndClose = useCallback(() => closeAndResolve(false), [closeAndResolve])
const sendAndClose = useCallback(() => closeAndResolve(true), [closeAndResolve])
Expand Down
40 changes: 40 additions & 0 deletions app/frontend/components/errorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Component, h} from 'preact'
import {captureException} from '@sentry/browser'
import {connect} from '../helpers/connect'
import actions from '../actions'
import UnexpectedErrorModal from './common/unexpectedErrorModal'

interface Props {
shouldShowUnexpectedErrorModal: boolean
}
class ErrorBoundary extends Component<Props, {}> {
state = {errorCaughtAtBoundary: null}

static getDerivedStateFromError(error) {
return {errorCaughtAtBoundary: error.message}
}

componentDidCatch(error) {
captureException(error)
}

render() {
const isUnhandledError = this.state.errorCaughtAtBoundary != null

return (
<span>
{!isUnhandledError && this.props.children}
{this.props.shouldShowUnexpectedErrorModal && (
<UnexpectedErrorModal reloadPageOnClose={isUnhandledError} />
)}
</span>
)
}
}

export default connect(
(state) => ({
shouldShowUnexpectedErrorModal: state.shouldShowUnexpectedErrorModal,
}),
actions
)(ErrorBoundary)

0 comments on commit 73d6742

Please sign in to comment.