Skip to content

Commit 6adc55d

Browse files
authored
Merge branch 'develop-postgres' into issue#2645
2 parents b12b94f + 9723078 commit 6adc55d

File tree

8 files changed

+116
-19
lines changed

8 files changed

+116
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Talawa Admin
22

3-
[💬 Join the community on Slack](https://github.com/PalisadoesFoundation/)
3+
💬 Join the community on Slack from our [Palisadoes Foundation GitHub Home Page](https://github.com/PalisadoesFoundation)
44

55
![talawa-logo-lite-200x200](https://github.com/PalisadoesFoundation/talawa-admin/assets/16875803/26291ec5-d3c1-4135-8bc7-80885dff613d)
66

public/locales/en/errors.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
"emailNotRegistered": "Email not registered",
88
"notFoundMsg": "Oops! The Page you requested was not found!",
99
"errorOccurredCouldntCreate": "An error occurred. Couldn't create {{entity}}",
10-
"errorLoading": "Error occured while loading {{entity}} data"
10+
"errorLoading": "Error occured while loading {{entity}} data",
11+
"invalidPhoneNumber": "Please enter a valid phone number",
12+
"invalidEducationGrade": "Please select a valid education grade",
13+
"invalidEmploymentStatus": "Please select a valid employment status",
14+
"invalidMaritalStatus": "Please select a valid marital status",
15+
"error400": "The submitted information is invalid. Please check your inputs and try again"
1116
}

public/locales/fr/errors.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
"emailNotRegistered": "Email non enregistré",
88
"notFoundMsg": "Oops! ",
99
"errorOccurredCouldntCreate": "Une erreur s'est produite. Impossible de créer {{entity}}",
10-
"errorLoading": "Une erreur s'est produite lors du chargement des données {{entity}}"
10+
"errorLoading": "Une erreur s'est produite lors du chargement des données {{entity}}",
11+
"invalidPhoneNumber": "Veuillez entrer un numéro de téléphone valide",
12+
"invalidEducationGrade": "Veuillez sélectionner un niveau d'études valide",
13+
"invalidEmploymentStatus": "Veuillez sélectionner un statut d'emploi valide",
14+
"invalidMaritalStatus": "Veuillez sélectionner un état matrimonial valide",
15+
"error400": "Réponse non réussie. Code d'état 400 reçu du serveur"
1116
}

public/locales/hi/errors.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
"emailNotRegistered": "ईमेल पंजीकृत नहीं है",
88
"notFoundMsg": "उफ़! ",
99
"errorOccurredCouldntCreate": "एक त्रुटि हुई। {{entity}} नहीं बना सके",
10-
"errorLoading": "{{entity}} डेटा लोड करते समय त्रुटि हुई"
10+
"errorLoading": "{{entity}} डेटा लोड करते समय त्रुटि हुई",
11+
"invalidPhoneNumber": "कृपया एक मान्य फोन-नंबर दर्ज करे",
12+
"invalidEducationGrade": "कृपया एक शिक्षा ग्रेड चुनें",
13+
"invalidEmploymentStatus": "कृपया वैध रोजगार स्थिति चुनें",
14+
"invalidMaritalStatus": "कृपया वैध वैवाहिक स्थिति चुनें",
15+
"error400": "आपकी जानकारी सहेजी नहीं जा सकी। कृपया अपनी प्रविष्टियों की जांच करें और पुनः प्रयास करें।"
1116
}

public/locales/sp/errors.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
"emailNotRegistered": "Email not registered",
88
"notFoundMsg": "Oops! The Page you requested was not found!",
99
"errorOccurredCouldntCreate": "Ocurrió un error. No se pudo crear {{entity}}",
10-
"errorLoading": "Ocurrió un error al cargar los datos de {{entity}}"
10+
"errorLoading": "Ocurrió un error al cargar los datos de {{entity}}",
11+
"invalidPhoneNumber": "Por favor, introduzca un número de teléfono válido",
12+
"invalidEducationGrade": "Por favor seleccione un grado de educación válido",
13+
"invalidEmploymentStatus": "Por favor seleccione un estado de empleo válido",
14+
"invalidMaritalStatus": "Por favor seleccione un estado civil válido",
15+
"error400": "Respuesta no exitosa. Se recibió el código de estado 400 del servidor"
1116
}

public/locales/zh/errors.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
"emailNotRegistered": "邮箱未注册",
88
"notFoundMsg": "哎呀!",
99
"errorOccurredCouldntCreate": "发生错误。 无法创建{{entity}}",
10-
"errorLoading": "加载{{entity}}数据时出错"
10+
"errorLoading": "加载{{entity}}数据时出错",
11+
"invalidPhoneNumber": "请选择一个有效的电话号码",
12+
"invalidEducationGrade": "请选择教育年级",
13+
"invalidEmploymentStatus": "请选择有效的就业状况",
14+
"invalidMaritalStatus": "请选择有效的婚姻状况",
15+
"error400": "响应不成功. 从服务器收到状态代码 400"
1116
}

src/utils/errorHandler.test.tsx

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,87 @@ jest.mock('react-toastify', () => ({
1111

1212
describe('Test if errorHandler is working properly', () => {
1313
const t: TFunction = (key: string) => key;
14-
const tErrors: TFunction = (key: string, options?: Record<string, unknown>) =>
15-
key;
14+
const tErrors: TFunction = (
15+
key: string,
16+
options?: Record<string, unknown>,
17+
) => {
18+
if (options) {
19+
console.log(`options are passed, but the function returns only ${key}`);
20+
}
21+
return key;
22+
};
1623

17-
it('should call toast.error with the correct message if error message is "Failed to fetch"', () => {
24+
beforeEach(() => {
25+
jest.clearAllMocks();
26+
});
27+
28+
it('should call toast.error with the correct message if error message is "Failed to fetch"', async () => {
1829
const error = new Error('Failed to fetch');
1930
errorHandler(t, error);
2031

2132
expect(toast.error).toHaveBeenCalledWith(tErrors('talawaApiUnavailable'));
2233
});
2334

24-
it('should call toast.error with the error message if it is not "Failed to fetch"', () => {
25-
const error = new Error('Some other error message');
35+
it('should call toast.error with the correct message if error message contains this substring "Value is not a valid phone number"', () => {
36+
const error = new Error('This value is not a valid phone number');
2637
errorHandler(t, error);
38+
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber'));
39+
});
40+
41+
test.each([
42+
['EducationGrade', 'invalidEducationGrade'],
43+
['EmploymentStatus', 'invalidEmploymentStatus'],
44+
['MaritalStatus', 'invalidMaritalStatus'],
45+
])('should handle invalid %s error', (field, expectedKey) => {
46+
const error = new Error(`This value does not exist in "${field}"`);
47+
errorHandler(t, error);
48+
expect(toast.error).toHaveBeenCalledWith(tErrors(expectedKey));
49+
});
50+
51+
it('should call toast.error with the correct message if error message contains this substring "status code 400"', () => {
52+
const error = new Error('Server responded with status code 400');
53+
errorHandler(t, error);
54+
55+
expect(toast.error).toHaveBeenCalledWith(tErrors('error400'));
56+
});
57+
58+
it('should handle error messages with different cases', () => {
59+
errorHandler(t, new Error('VALUE IS NOT A VALID PHONE NUMBER'));
60+
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber'));
61+
62+
errorHandler(t, new Error('This Value Does Not Exist in "EducationGrade"'));
63+
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidEducationGrade'));
64+
});
2765

66+
it('should call toast.error with the error message if it is an instance of error but have not matched any error message patterns', () => {
67+
const error = new Error('Bandhan sent an error message');
68+
errorHandler(t, error);
2869
expect(toast.error).toHaveBeenCalledWith(error.message);
2970
});
3071

72+
it('should handle different types for the first parameter while still showing error messages', () => {
73+
errorHandler(undefined, new Error('Some error'));
74+
expect(toast.error).toHaveBeenCalled();
75+
76+
errorHandler(null, new Error('Some error'));
77+
expect(toast.error).toHaveBeenCalled();
78+
79+
errorHandler({}, new Error('Some error'));
80+
expect(toast.error).toHaveBeenCalled();
81+
});
82+
83+
it('should handle non-null but non-Error objects for the error parameter', () => {
84+
errorHandler(t, { message: 'Error message in object' });
85+
expect(toast.error).toHaveBeenCalledWith(
86+
tErrors('unknownError', { msg: { message: 'Error message in object' } }),
87+
);
88+
89+
errorHandler(t, 'Direct error message');
90+
expect(toast.error).toHaveBeenCalledWith(
91+
tErrors('unknownError', { msg: 'Direct error message' }),
92+
);
93+
});
94+
3195
it('should call toast.error with the error message if error object is falsy', () => {
3296
const error = null;
3397
errorHandler(t, error);

src/utils/errorHandler.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ import i18n from './i18n';
55
/**
66
This function is used to handle api errors in the application.
77
It takes in the error object and displays the error message to the user.
8-
If the error is due to the Talawa API being unavailable, it displays a custom message.
8+
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
99
*/
1010
export const errorHandler = (a: unknown, error: unknown): void => {
1111
const tErrors: TFunction = i18n.getFixedT(null, 'errors');
1212
if (error instanceof Error) {
13-
switch (error.message) {
14-
case 'Failed to fetch':
15-
toast.error(tErrors('talawaApiUnavailable') as string);
16-
break;
17-
// Add more cases as needed
18-
default:
19-
toast.error(error.message);
13+
const errorMessage = error.message;
14+
if (errorMessage === 'Failed to fetch') {
15+
toast.error(tErrors('talawaApiUnavailable'));
16+
} else if (errorMessage.match(/value is not a valid phone number/i)) {
17+
toast.error(tErrors('invalidPhoneNumber'));
18+
} else if (errorMessage.match(/does not exist in "EducationGrade"/i)) {
19+
toast.error(tErrors('invalidEducationGrade'));
20+
} else if (errorMessage.match(/does not exist in "EmploymentStatus"/i)) {
21+
toast.error(tErrors('invalidEmploymentStatus'));
22+
} else if (errorMessage.match(/does not exist in "MaritalStatus"/i)) {
23+
toast.error(tErrors('invalidMaritalStatus'));
24+
} else if (errorMessage.match(/status code 400/i)) {
25+
toast.error(tErrors('error400'));
26+
} else {
27+
toast.error(errorMessage);
2028
}
2129
} else {
2230
toast.error(tErrors('unknownError', { msg: error }) as string);

0 commit comments

Comments
 (0)