ErrorBoundary: always display error messages

This commit is contained in:
Alex Gleason 2024-11-12 21:15:30 -06:00
parent 8a4e85010d
commit aceaa90b96
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 16 additions and 103 deletions

View File

@ -1,71 +0,0 @@
import { useState } from 'react';
import { FormattedMessage } from 'react-intl';
import Button from 'soapbox/components/ui/button.tsx';
import FormActions from 'soapbox/components/ui/form-actions.tsx';
import FormGroup from 'soapbox/components/ui/form-group.tsx';
import Form from 'soapbox/components/ui/form.tsx';
import Text from 'soapbox/components/ui/text.tsx';
import Textarea from 'soapbox/components/ui/textarea.tsx';
import { useOwnAccount } from 'soapbox/hooks/useOwnAccount.ts';
import { captureSentryFeedback } from 'soapbox/sentry.ts';
interface ISentryFeedbackForm {
eventId: string;
}
/** Accept feedback for the given Sentry event. */
const SentryFeedbackForm: React.FC<ISentryFeedbackForm> = ({ eventId }) => {
const { account } = useOwnAccount();
const [feedback, setFeedback] = useState<string>();
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
const [isSubmitted, setIsSubmitted] = useState<boolean>(false);
const handleFeedbackChange: React.ChangeEventHandler<HTMLTextAreaElement> = (e) => {
setFeedback(e.target.value);
};
const handleSubmitFeedback: React.FormEventHandler = async (_e) => {
if (!feedback || !eventId) return;
setIsSubmitting(true);
await captureSentryFeedback({
name: account?.acct,
associatedEventId: eventId,
message: feedback,
}).catch(console.error);
setIsSubmitted(true);
};
if (isSubmitted) {
return (
<Text align='center'>
<FormattedMessage id='alert.unexpected.thanks' defaultMessage='Thanks for your feedback!' />
</Text>
);
}
return (
<Form onSubmit={handleSubmitFeedback}>
<FormGroup>
<Textarea
value={feedback}
onChange={handleFeedbackChange}
placeholder='Anything you can tell us about what happened?'
disabled={isSubmitting}
autoGrow
/>
</FormGroup>
<FormActions>
<Button type='submit' className='mx-auto' disabled={!feedback || isSubmitting}>
<FormattedMessage id='alert.unexpected.submit_feedback' defaultMessage='Submit Feedback' />
</Button>
</FormActions>
</Form>
);
};
export default SentryFeedbackForm;

View File

@ -2,7 +2,6 @@ import { type ErrorInfo, useRef, useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary'; import { ErrorBoundary } from 'react-error-boundary';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { NODE_ENV } from 'soapbox/build-config.ts';
import HStack from 'soapbox/components/ui/hstack.tsx'; import HStack from 'soapbox/components/ui/hstack.tsx';
import Stack from 'soapbox/components/ui/stack.tsx'; import Stack from 'soapbox/components/ui/stack.tsx';
import Text from 'soapbox/components/ui/text.tsx'; import Text from 'soapbox/components/ui/text.tsx';
@ -13,7 +12,6 @@ import KVStore from 'soapbox/storage/kv-store.ts';
import sourceCode from 'soapbox/utils/code.ts'; import sourceCode from 'soapbox/utils/code.ts';
import { unregisterSW } from 'soapbox/utils/sw.ts'; import { unregisterSW } from 'soapbox/utils/sw.ts';
import SentryFeedbackForm from './sentry-feedback-form.tsx';
import SiteLogo from './site-logo.tsx'; import SiteLogo from './site-logo.tsx';
interface ISiteErrorBoundary { interface ISiteErrorBoundary {
@ -22,16 +20,13 @@ 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, sentryDsn } = useSoapboxConfig(); const { links } = 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 | null | undefined>(); const [componentStack, setComponentStack] = useState<string | null | undefined>();
const [browser, setBrowser] = useState<Bowser.Parser.Parser>(); const [browser, setBrowser] = useState<Bowser.Parser.Parser>();
const [sentryEventId, setSentryEventId] = useState<string>();
const sentryEnabled = Boolean(sentryDsn);
const isProduction = NODE_ENV === 'production';
const errorText = String(error) + componentStack; const errorText = String(error) + componentStack;
const clearCookies: React.MouseEventHandler = (e) => { const clearCookies: React.MouseEventHandler = (e) => {
@ -64,7 +59,6 @@ const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
ErrorBoundary: 'yes', ErrorBoundary: 'yes',
}, },
}) })
.then((eventId) => setSentryEventId(eventId))
.catch(console.error); .catch(console.error);
import('bowser') import('bowser')
@ -127,30 +121,22 @@ const SiteErrorBoundary: React.FC<ISiteErrorBoundary> = ({ children }) => {
</div> </div>
<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) ? ( {errorText && (
(sentryEnabled && sentryEventId) && ( <Textarea
<SentryFeedbackForm eventId={sentryEventId} /> ref={textarea}
) value={errorText}
) : ( onClick={handleCopy}
<> isCodeEditor
{errorText && ( rows={12}
<Textarea readOnly
ref={textarea} />
value={errorText} )}
onClick={handleCopy}
isCodeEditor
rows={12}
readOnly
/>
)}
{browser && ( {browser && (
<Stack> <Stack>
<Text weight='semibold'><FormattedMessage id='alert.unexpected.browser' defaultMessage='Browser' /></Text> <Text weight='semibold'><FormattedMessage id='alert.unexpected.browser' defaultMessage='Browser' /></Text>
<Text theme='muted'>{browser.getBrowserName()} {browser.getBrowserVersion()}</Text> <Text theme='muted'>{browser.getBrowserName()} {browser.getBrowserVersion()}</Text>
</Stack> </Stack>
)}
</>
)} )}
</div> </div>
</div> </div>

View File

@ -202,8 +202,6 @@
"alert.unexpected.links.support": "Support", "alert.unexpected.links.support": "Support",
"alert.unexpected.message": "Something went wrong.", "alert.unexpected.message": "Something went wrong.",
"alert.unexpected.return_home": "Return Home", "alert.unexpected.return_home": "Return Home",
"alert.unexpected.submit_feedback": "Submit Feedback",
"alert.unexpected.thanks": "Thanks for your feedback!",
"aliases.account.add": "Create alias", "aliases.account.add": "Create alias",
"aliases.account_label": "Old account:", "aliases.account_label": "Old account:",
"aliases.aliases_list_delete": "Unlink alias", "aliases.aliases_list_delete": "Unlink alias",