Skip to content

Commit 4ba9bb8

Browse files
committed
Attempt to improve handling of top-level unrecoverable fatal errors
1 parent 8245fcd commit 4ba9bb8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/components/appBootstrap/MultiversalAppBootstrap.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createLogger } from '@unly/utils-simple-logger';
44
import { ThemeProvider } from 'emotion-theming';
55
import { i18n } from 'i18next';
66
import React from 'react';
7+
import ErrorPage from '../../pages/_error';
78
import customerContext from '../../stores/customerContext';
89
import i18nContext from '../../stores/i18nContext';
910
import { Theme } from '../../types/data/Theme';
@@ -13,6 +14,7 @@ import { SSGPageProps } from '../../types/pageProps/SSGPageProps';
1314
import { SSRPageProps } from '../../types/pageProps/SSRPageProps';
1415
import { initCustomerTheme } from '../../utils/data/theme';
1516
import i18nextLocize from '../../utils/i18n/i18nextLocize';
17+
import DefaultErrorLayout from '../errors/DefaultErrorLayout';
1618
import BrowserPageBootstrap, { BrowserPageBootstrapProps } from './BrowserPageBootstrap';
1719
import ServerPageBootstrap, { ServerPageBootstrapProps } from './ServerPageBootstrap';
1820
import UniversalGlobalStyles from './UniversalGlobalStyles';
@@ -55,6 +57,32 @@ const MultiversalAppBootstrap: React.FunctionComponent<Props> = (props): JSX.Ele
5557
lang,
5658
locale,
5759
}: MultiversalPageProps = pageProps;
60+
61+
if (!customer || !i18nTranslations || !lang || !locale) {
62+
// Unrecoverable error, we can't even display the layout because we don't have the minimal required information to properly do so
63+
// This most likely means something went wrong, and we must display the error page in such case
64+
if (!props.err) {
65+
// If the error wasn't detected by Next, then we log it to Sentry to make sure we'll be notified
66+
67+
Sentry.withScope((scope): void => {
68+
scope.setContext('props', props);
69+
Sentry.captureMessage(`Unexpected fatal error happened, the app cannot render properly, fallback to the Error page. Check props.`, Sentry.Severity.Warning);
70+
});
71+
72+
} else {
73+
// If an error was detected by Next, then it means the current state is due to a top-level that was caught before
74+
// We don't have anything to do, as it's automatically logged into Sentry
75+
}
76+
77+
return (
78+
<ErrorPage err={props.err} statusCode={500} isReadyToRender={true}>
79+
<DefaultErrorLayout
80+
error={props.err}
81+
/>
82+
</ErrorPage>
83+
);
84+
}
85+
5886
const i18nextInstance: i18n = i18nextLocize(lang, i18nTranslations); // Apply i18next configuration with Locize backend
5987
const theme: Theme = initCustomerTheme(customer);
6088

0 commit comments

Comments
 (0)