Skip to content

Commit

Permalink
fix: used regular expressions and added a clear mocking function
Browse files Browse the repository at this point in the history
  • Loading branch information
bandhan-majumder committed Dec 2, 2024
1 parent 06a9a64 commit 710103b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/utils/errorHandler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ describe('Test if errorHandler is working properly', () => {
return key;
};

it('should call toast.error with the correct message if error message is "Failed to fetch"', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should call toast.error with the correct message if error message is "Failed to fetch"', async () => {
const error = new Error('Failed to fetch');
errorHandler(t, error);

Expand All @@ -31,7 +35,7 @@ describe('Test if errorHandler is working properly', () => {
it('should call toast.error with the correct message if error message contains this substring "Value is not a valid phone number"', () => {
const error = new Error('This value is not a valid phone number');
errorHandler(t, error);

console.log(toast.error);
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber'));
});

Expand Down
12 changes: 6 additions & 6 deletions src/utils/errorHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import i18n from './i18n';
/**
This function is used to handle api errors in the application.
It takes in the error object and displays the error message to the user.
If the error is due to the Talawa API being unavailable, it displays a custom message.
If the error is due to the Talawa API being unavailable, it displays a custom message. And for other error cases, it is using regular expression (case-insensitive) to match and show valid messages
*/
export const errorHandler = (a: unknown, error: unknown): void => {
const tErrors: TFunction = i18n.getFixedT(null, 'errors');
if (error instanceof Error) {
const errorMessage = error.message;
if (errorMessage === 'Failed to fetch') {
toast.error(tErrors('talawaApiUnavailable'));
} else if (errorMessage.includes('Value is not a valid phone number')) {
} else if (errorMessage.match(/value is not a valid phone number/i)) {
toast.error(tErrors('invalidPhoneNumber'));
} else if (errorMessage.includes('does not exist in "EducationGrade"')) {
} else if (errorMessage.match(/does not exist in "EducationGrade"/i)) {
toast.error(tErrors('invalidEducationGrade'));
} else if (errorMessage.includes('does not exist in "EmploymentStatus"')) {
} else if (errorMessage.match(/does not exist in "EmploymentStatus"/i)) {
toast.error(tErrors('invalidEmploymentStatus'));
} else if (errorMessage.includes('does not exist in "MaritalStatus"')) {
} else if (errorMessage.match(/does not exist in "MaritalStatus"/i)) {
toast.error(tErrors('invalidMaritalStatus'));
} else if (errorMessage.includes('status code 400')) {
} else if (errorMessage.match(/status code 400/i)) {
toast.error(tErrors('error400'));
} else {
toast.error(errorMessage);
Expand Down

0 comments on commit 710103b

Please sign in to comment.