SiteErrorBoundary: rework conditionals for Sentry and production

This commit is contained in:
Alex Gleason 2023-10-21 15:52:37 -05:00
parent 5bbce463e7
commit c33ff771da
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 35 additions and 32 deletions

View File

@ -18,13 +18,14 @@ interface ISiteErrorBoundary {
/** Application-level error boundary. Fills the whole screen. */ /** Application-level error boundary. Fills the whole screen. */
const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => { const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
const { links } = useSoapboxConfig(); const { links, sentryDsn } = useSoapboxConfig();
const textarea = useRef<HTMLTextAreaElement>(null); const textarea = useRef<HTMLTextAreaElement>(null);
const [error, setError] = useState<unknown>(); const [error, setError] = useState<unknown>();
const [componentStack, setComponentStack] = useState<string>(); const [componentStack, setComponentStack] = useState<string>();
const [browser, setBrowser] = useState<Bowser.Parser.Parser>(); const [browser, setBrowser] = useState<Bowser.Parser.Parser>();
const sentryEnabled = Boolean(sentryDsn);
const isProduction = NODE_ENV === 'production'; const isProduction = NODE_ENV === 'production';
const errorText = String(error) + componentStack; const errorText = String(error) + componentStack;
@ -52,7 +53,6 @@ const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
setError(error); setError(error);
setComponentStack(info.componentStack); setComponentStack(info.componentStack);
if (isProduction) {
captureSentryException(error, { captureSentryException(error, {
tags: { tags: {
// Allow page crashes to be easily searched in Sentry. // Allow page crashes to be easily searched in Sentry.
@ -64,7 +64,6 @@ const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
.then(({ default: Bowser }) => setBrowser(Bowser.getParser(window.navigator.userAgent))) .then(({ default: Bowser }) => setBrowser(Bowser.getParser(window.navigator.userAgent)))
.catch(() => {}); .catch(() => {});
} }
}
function goHome() { function goHome() {
location.href = '/'; location.href = '/';
@ -116,8 +115,11 @@ const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
</div> </div>
</div> </div>
{!isProduction && (
<div className='mx-auto max-w-lg space-y-4 py-16'> <div className='mx-auto max-w-lg space-y-4 py-16'>
{(isProduction && sentryEnabled) ? (
<>{/* TODO: Sentry report form. */}</>
) : (
<>
{errorText && ( {errorText && (
<textarea <textarea
ref={textarea} ref={textarea}
@ -135,9 +137,10 @@ const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
<Text theme='muted'>{browser.getBrowserName()} {browser.getBrowserVersion()}</Text> <Text theme='muted'>{browser.getBrowserName()} {browser.getBrowserVersion()}</Text>
</Stack> </Stack>
)} )}
</div> </>
)} )}
</div> </div>
</div>
</main> </main>
<footer className='mx-auto w-full max-w-7xl shrink-0 px-4 sm:px-6 lg:px-8'> <footer className='mx-auto w-full max-w-7xl shrink-0 px-4 sm:px-6 lg:px-8'>