From 40b4fd3be9d9f11bee401c972e1efa07aaad1dcf Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 12 Dec 2024 19:57:01 +0200 Subject: [PATCH] Adds generic error handling --- .../core/src/js/feedback/FeedbackForm.tsx | 7 ++++-- .../src/js/feedback/FeedbackForm.types.ts | 5 ++++ packages/core/src/js/feedback/defaults.ts | 2 ++ packages/core/src/js/feedback/utils.ts | 8 +++++-- .../core/test/feedback/FeedbackForm.test.tsx | 23 +++++++++++++++++-- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/packages/core/src/js/feedback/FeedbackForm.tsx b/packages/core/src/js/feedback/FeedbackForm.tsx index 6b3e31fad..b561dadd3 100644 --- a/packages/core/src/js/feedback/FeedbackForm.tsx +++ b/packages/core/src/js/feedback/FeedbackForm.tsx @@ -55,14 +55,17 @@ export class FeedbackForm extends React.Component { + 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}`); }); }; diff --git a/packages/core/src/js/feedback/FeedbackForm.types.ts b/packages/core/src/js/feedback/FeedbackForm.types.ts index 3710c42e7..3d7aeb117 100644 --- a/packages/core/src/js/feedback/FeedbackForm.types.ts +++ b/packages/core/src/js/feedback/FeedbackForm.types.ts @@ -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; } /** diff --git a/packages/core/src/js/feedback/defaults.ts b/packages/core/src/js/feedback/defaults.ts index 714bfeaa5..9499cdd5b 100644 --- a/packages/core/src/js/feedback/defaults.ts +++ b/packages/core/src/js/feedback/defaults.ts @@ -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 = { // FeedbackCallbacks @@ -56,4 +57,5 @@ export const defaultConfiguration: Partial = { emailError: EMAIL_ERROR, successMessageText: SUCCESS_MESSAGE_TEXT, networkError: CONNECTIONS_ERROR_TEXT, + genericError: GENERIC_ERROR_TEXT, }; diff --git a/packages/core/src/js/feedback/utils.ts b/packages/core/src/js/feedback/utils.ts index 3b8424906..fb46b1f86 100644 --- a/packages/core/src/js/feedback/utils.ts +++ b/packages/core/src/js/feedback/utils.ts @@ -1,4 +1,8 @@ -export const checkInternetConnection = async (onConnected: () => void, onDisconnected: () => void): Promise => { +export const checkInternetConnection = async ( + onConnected: () => void, + onDisconnected: () => void, + onError: () => void, +): Promise => { try { const response = await fetch('https://sentry.io', { method: 'HEAD' }); if (response.ok) { @@ -7,7 +11,7 @@ export const checkInternetConnection = async (onConnected: () => void, onDisconn onDisconnected(); } } catch (error) { - onDisconnected(); + onError(); } }; diff --git a/packages/core/test/feedback/FeedbackForm.test.tsx b/packages/core/test/feedback/FeedbackForm.test.tsx index 55b8acf11..ff6922b48 100644 --- a/packages/core/test/feedback/FeedbackForm.test.tsx +++ b/packages/core/test/feedback/FeedbackForm.test.tsx @@ -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(); }); }); @@ -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(); }); @@ -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(); + + fireEvent.changeText(getByPlaceholderText(defaultProps.namePlaceholder), 'John Doe'); + fireEvent.changeText(getByPlaceholderText(defaultProps.emailPlaceholder), 'john.doe@example.com'); + 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();