Skip to content

Commit c014ccc

Browse files
committed
Only catch errors thrown from the captureFeedback function
1 parent 634df43 commit c014ccc

File tree

5 files changed

+11
-68
lines changed

5 files changed

+11
-68
lines changed

packages/core/src/js/feedback/FeedbackForm.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { sentryLogo } from './branding';
2020
import { defaultConfiguration } from './defaults';
2121
import defaultStyles from './FeedbackForm.styles';
2222
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';
23-
import { checkInternetConnection, isValidEmail } from './utils';
23+
import { isValidEmail } from './utils';
2424

2525
/**
2626
* @beta
@@ -76,22 +76,18 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
7676
associatedEventId: eventId,
7777
};
7878

79-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
80-
checkInternetConnection(() => { // onConnected
79+
try {
8180
this.setState({ isVisible: false });
8281
captureFeedback(userFeedback);
8382
onSubmitSuccess({ name: trimmedName, email: trimmedEmail, message: trimmedDescription, attachments: undefined });
8483
Alert.alert(text.successMessageText);
8584
onFormSubmitted();
86-
}, () => { // onDisconnected
87-
Alert.alert(text.errorTitle, text.networkError);
88-
logger.error(`Feedback form submission failed: ${text.networkError}`);
89-
}, () => { // onError
90-
const errorString = `Feedback form submission failed: ${text.genericError}`;
85+
} catch (error) {
86+
const errorString = `Feedback form submission failed: ${error}`;
9187
onSubmitError(new Error(errorString));
9288
Alert.alert(text.errorTitle, text.genericError);
93-
logger.error(errorString);
94-
});
89+
logger.error(`Feedback form submission failed: ${error}`);
90+
}
9591
};
9692

9793
/**

packages/core/src/js/feedback/FeedbackForm.types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ export interface FeedbackTextConfiguration {
129129
emailError?: string;
130130

131131
/**
132-
* Message when there is a network error
133-
*/
134-
networkError?: string;
135-
136-
/**
137-
* Message when there is a network error
132+
* Message when there is a generic error
138133
*/
139134
genericError?: string;
140135
}

packages/core/src/js/feedback/defaults.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const ERROR_TITLE = 'Error';
1616
const FORM_ERROR = 'Please fill out all required fields.';
1717
const EMAIL_ERROR = 'Please enter a valid email address.';
1818
const SUCCESS_MESSAGE_TEXT = 'Thank you for your report!';
19-
const CONNECTIONS_ERROR_TEXT = 'Unable to send Feedback due to network issues.';
2019
const GENERIC_ERROR_TEXT = 'Unable to send feedback due to an unexpected error.';
2120

2221
export const defaultConfiguration: Partial<FeedbackFormProps> = {
@@ -70,6 +69,5 @@ export const defaultConfiguration: Partial<FeedbackFormProps> = {
7069
formError: FORM_ERROR,
7170
emailError: EMAIL_ERROR,
7271
successMessageText: SUCCESS_MESSAGE_TEXT,
73-
networkError: CONNECTIONS_ERROR_TEXT,
7472
genericError: GENERIC_ERROR_TEXT,
7573
};

packages/core/src/js/feedback/utils.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
export const checkInternetConnection = async (
2-
onConnected: () => void,
3-
onDisconnected: () => void,
4-
onError: () => void,
5-
): Promise<void> => {
6-
try {
7-
const response = await fetch('https://sentry.io', { method: 'HEAD' });
8-
if (response.ok) {
9-
onConnected();
10-
} else {
11-
onDisconnected();
12-
}
13-
} catch (error) {
14-
onError();
15-
}
16-
};
17-
181
export const isValidEmail = (email: string): boolean => {
192
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
203
return emailRegex.test(email);

packages/core/test/feedback/FeedbackForm.test.tsx

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Alert } from 'react-native';
55

66
import { FeedbackForm } from '../../src/js/feedback/FeedbackForm';
77
import type { FeedbackFormProps } from '../../src/js/feedback/FeedbackForm.types';
8-
import { checkInternetConnection } from '../../src/js/feedback/utils';
98

109
const mockOnFormClose = jest.fn();
1110
const mockOnSubmitSuccess = jest.fn();
@@ -26,10 +25,6 @@ jest.mock('@sentry/core', () => ({
2625
})),
2726
lastEventId: jest.fn(),
2827
}));
29-
jest.mock('../../src/js/feedback/utils', () => ({
30-
...jest.requireActual('../../src/js/feedback/utils'),
31-
checkInternetConnection: jest.fn(),
32-
}));
3328

3429
const defaultProps: FeedbackFormProps = {
3530
onFormClose: mockOnFormClose,
@@ -50,16 +45,10 @@ const defaultProps: FeedbackFormProps = {
5045
formError: 'Please fill out all required fields.',
5146
emailError: 'The email address is not valid.',
5247
successMessageText: 'Feedback success',
53-
networkError: 'Network error',
5448
genericError: 'Generic error',
5549
};
5650

5751
describe('FeedbackForm', () => {
58-
beforeEach(() => {
59-
(checkInternetConnection as jest.Mock).mockImplementation((onConnected, _onDisconnected, _onError) => {
60-
onConnected();
61-
});
62-
});
6352
afterEach(() => {
6453
jest.clearAllMocks();
6554
});
@@ -163,27 +152,9 @@ describe('FeedbackForm', () => {
163152
});
164153
});
165154

166-
it('shows an error message when there is no network connection', async () => {
167-
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, onDisconnected, _onError) => {
168-
onDisconnected();
169-
});
170-
171-
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />);
172-
173-
fireEvent.changeText(getByPlaceholderText(defaultProps.namePlaceholder), 'John Doe');
174-
fireEvent.changeText(getByPlaceholderText(defaultProps.emailPlaceholder), '[email protected]');
175-
fireEvent.changeText(getByPlaceholderText(defaultProps.messagePlaceholder), 'This is a feedback message.');
176-
177-
fireEvent.press(getByText(defaultProps.submitButtonLabel));
178-
179-
await waitFor(() => {
180-
expect(Alert.alert).toHaveBeenCalledWith(defaultProps.errorTitle, defaultProps.networkError);
181-
});
182-
});
183-
184155
it('shows an error message when there is a generic connection', async () => {
185-
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, _onDisconnected, onError) => {
186-
onError();
156+
(captureFeedback as jest.Mock).mockImplementationOnce(() => {
157+
throw new Error('Test error');
187158
});
188159

189160
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />);
@@ -200,8 +171,8 @@ describe('FeedbackForm', () => {
200171
});
201172

202173
it('calls onSubmitError when there is an error', async () => {
203-
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, _onDisconnected, onError) => {
204-
onError();
174+
(captureFeedback as jest.Mock).mockImplementationOnce(() => {
175+
throw new Error('Test error');
205176
});
206177

207178
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />);

0 commit comments

Comments
 (0)