Skip to content

Commit

Permalink
Capture failed turnstile challenges to sentry (#738)
Browse files Browse the repository at this point in the history
* Capture failed turnstile challenges to sentry

* Address comments

* prettier

* warning
  • Loading branch information
henrikskog authored Sep 5, 2024
1 parent f1f08ae commit 5b38a07
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/events/components/Attendance/AttendButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useToast } from 'core/utils/toast/useToast';
import { unwrapResult } from '@reduxjs/toolkit';
import style from './attendance.less';
import { ConfirmModal } from '../../../common/components/Modal';
import * as Sentry from '@sentry/browser';
import { getUser } from '../../../authentication/api';

interface IAttendButtonProps {
eventId: number;
Expand All @@ -23,10 +25,7 @@ const FAILED_CAPTCHA_ERROR_TEXT = (
<ul>
<li>Last inn siden på nytt og prøv igjen.</li>
<li>Prøv en annen nettleser. Vi vet at det har vært problemer med Firefox.</li>
<li>
Hvis du fortsatt får feil, så send en e-post til [email protected] så vi melder deg på manuelt. Vi setter pris på om du
kan oppgi eventuell feilmelding og hvilken nettleser og operativsystem du bruker for å kunne skjønne bedre hva som går galt.
</li>
<li>Hvis du fortsatt får feil, så send en e-post til [email protected] så vi melder deg på manuelt.</li>
</ul>
</>
);
Expand All @@ -44,7 +43,8 @@ const AttendButton: FC<IAttendButtonProps> = (props: IAttendButtonProps) => {
unwrapResult(action);
addToast('Du har blitt meldt på arrangementet');
} catch (err) {
addToast(`Noe gikk galt under påmeldelse av arrangement, ERROR: ${err.message}`, { type: 'error' });
const errorMessage = err instanceof Error ? err.message : 'Ukjent feil';
addToast(`Noe gikk galt under påmeldelse av arrangement, ERROR: ${errorMessage}`, { type: 'error' });
}
}
};
Expand All @@ -55,6 +55,19 @@ const AttendButton: FC<IAttendButtonProps> = (props: IAttendButtonProps) => {
toggleModal={toggleModal}
setCaptcha={signUp}
errorText={FAILED_CAPTCHA_ERROR_TEXT}
onError={async (error) => {
const user = await getUser();
Sentry.captureEvent({
message: 'User failed turnstile challenge when signing up for event.',
level: Sentry.Severity.Warning,
extra: {
error: error,
},
user: {
id: user?.profile.sub,
},
});
}}
/>
);
const onConfirmModalClose = (retValue: boolean) => {
Expand Down
8 changes: 5 additions & 3 deletions src/events/components/Attendance/CaptchaModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ interface ICaptchaModalProps {
toggleModal: () => void;
setCaptcha: (token: string | null) => void;
errorText?: React.ReactNode | string;
onError: (error: Error) => void;
}

const CaptchaModal: FC<ICaptchaModalProps> = (props: ICaptchaModalProps) => {
const { showModal, toggleModal, setCaptcha, header, text, errorText } = props;
const { showModal, toggleModal, setCaptcha, header, text, errorText, onError } = props;
const [showErrorText, setShowErrorText] = useState(true);
const [turnstileError, setTurnstileError] = useState<string | null>(null);

Expand All @@ -25,8 +26,9 @@ const CaptchaModal: FC<ICaptchaModalProps> = (props: ICaptchaModalProps) => {
//TODO Do something with unvalid token?
};

const onError = (error: Error) => {
const _onError = (error: Error) => {
console.log('Error from captcha failure:', error);
onError(error);
setShowErrorText(true);
setTurnstileError(error.message || 'Ingen feilmelding');
};
Expand All @@ -39,7 +41,7 @@ const CaptchaModal: FC<ICaptchaModalProps> = (props: ICaptchaModalProps) => {
<p>{text}</p>
{showErrorText && <p>{errorText}</p>}
{turnstileError && <p>Error message: {turnstileError}</p>}
<Turnstile sitekey={OW4_TURNSTILE_PUBLIC_KEY} onVerify={validCaptcha} onError={onError} />
<Turnstile sitekey={OW4_TURNSTILE_PUBLIC_KEY} onVerify={validCaptcha} onError={_onError} />
</Modal>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if (__CLIENT__ && __PROD__) {

Sentry.init({
dsn: OWF_SENTRY_DSN,
environment: process.env.NODE_ENV,
});

type Props = AppProps & ReduxWrapperAppProps<State>;
Expand Down

0 comments on commit 5b38a07

Please sign in to comment.