Skip to content

Commit

Permalink
Adds generic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antonis committed Dec 12, 2024
1 parent b42e6f6 commit 40b4fd3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
7 changes: 5 additions & 2 deletions packages/core/src/js/feedback/FeedbackForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
checkInternetConnection(() => {
checkInternetConnection(() => { // onConnected
onFormClose();
this.setState({ isVisible: false });
captureFeedback(userFeedback);
Alert.alert(text.successMessageText);
}, () => {
}, () => { // onDisconnected
Alert.alert(text.errorTitle, text.networkError);
logger.error(`Feedback form submission failed: ${text.networkError}`);
}, () => { // onError
Alert.alert(text.errorTitle, text.genericError);
logger.error(`Feedback form submission failed: ${text.genericError}`);
});
};

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/js/feedback/FeedbackForm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export interface FeedbackTextConfiguration {
* Message when there is a network error
*/
networkError?: string;

/**
* Message when there is a network error
*/
genericError?: string;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/js/feedback/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const FORM_ERROR = 'Please fill out all required fields.';
const EMAIL_ERROR = 'Please enter a valid email address.';
const SUCCESS_MESSAGE_TEXT = 'Thank you for your report!';
const CONNECTIONS_ERROR_TEXT = 'Unable to send Feedback due to network issues.';
const GENERIC_ERROR_TEXT = 'Unable to send feedback due to an unexpected error.';

export const defaultConfiguration: Partial<FeedbackFormProps> = {
// FeedbackCallbacks
Expand Down Expand Up @@ -56,4 +57,5 @@ export const defaultConfiguration: Partial<FeedbackFormProps> = {
emailError: EMAIL_ERROR,
successMessageText: SUCCESS_MESSAGE_TEXT,
networkError: CONNECTIONS_ERROR_TEXT,
genericError: GENERIC_ERROR_TEXT,
};
8 changes: 6 additions & 2 deletions packages/core/src/js/feedback/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const checkInternetConnection = async (onConnected: () => void, onDisconnected: () => void): Promise<void> => {
export const checkInternetConnection = async (
onConnected: () => void,
onDisconnected: () => void,
onError: () => void,
): Promise<void> => {
try {
const response = await fetch('https://sentry.io', { method: 'HEAD' });
if (response.ok) {
Expand All @@ -7,7 +11,7 @@ export const checkInternetConnection = async (onConnected: () => void, onDisconn
onDisconnected();
}
} catch (error) {
onDisconnected();
onError();
}
};

Expand Down
23 changes: 21 additions & 2 deletions packages/core/test/feedback/FeedbackForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ const defaultProps: FeedbackFormProps = {
emailError: 'The email address is not valid.',
successMessageText: 'Feedback success',
networkError: 'Network error',
genericError: 'Generic error',
};

describe('FeedbackForm', () => {
beforeEach(() => {
(checkInternetConnection as jest.Mock).mockImplementation((onConnected, _) => {
(checkInternetConnection as jest.Mock).mockImplementation((onConnected, _onDisconnected, _onError) => {
onConnected();
});
});
Expand Down Expand Up @@ -138,7 +139,7 @@ describe('FeedbackForm', () => {
});

it('shows an error message when there is no network connection', async () => {
(checkInternetConnection as jest.Mock).mockImplementationOnce((_, onDisconnected) => {
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, onDisconnected, _onError) => {
onDisconnected();
});

Expand All @@ -155,6 +156,24 @@ describe('FeedbackForm', () => {
});
});

it('shows an error message when there is a generic connection', async () => {
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, _onDisconnected, onError) => {
onError();
});

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

fireEvent.changeText(getByPlaceholderText(defaultProps.namePlaceholder), 'John Doe');
fireEvent.changeText(getByPlaceholderText(defaultProps.emailPlaceholder), '[email protected]');
fireEvent.changeText(getByPlaceholderText(defaultProps.messagePlaceholder), 'This is a feedback message.');

fireEvent.press(getByText(defaultProps.submitButtonLabel));

await waitFor(() => {
expect(Alert.alert).toHaveBeenCalledWith(defaultProps.errorTitle, defaultProps.networkError);
});
});

it('calls onFormClose when the form is submitted successfully', async () => {
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />);

Expand Down

0 comments on commit 40b4fd3

Please sign in to comment.