From 1df1df616f50cf96553a99efaf77e300deeb6989 Mon Sep 17 00:00:00 2001 From: yug-shah0106 <57866661+yug-shah0106@users.noreply.github.com> Date: Tue, 31 May 2022 10:16:06 +0530 Subject: [PATCH] [issue 5222] remove redundant code used to check API response (#5223) * [issue 5222] remove redundant code used to check API response * minor fix --- .../ui/src/axiosAPIs/dashboardAPI.ts | 4 +-- .../main/resources/ui/src/axiosAPIs/index.js | 26 +++++++++++++- .../resources/ui/src/axiosAPIs/pipelineAPI.ts | 4 +-- .../resources/ui/src/axiosAPIs/tableAPI.ts | 4 +-- .../resources/ui/src/axiosAPIs/teamsAPI.ts | 6 ++-- .../resources/ui/src/axiosAPIs/topicsAPI.ts | 4 +-- .../resources/ui/src/axiosAPIs/userAPI.ts | 4 +-- .../src/components/Users/Users.component.tsx | 6 +--- .../CreateUserPage.component.tsx | 6 +--- .../pages/MyDataPage/MyDataPage.component.tsx | 36 ++++--------------- .../pages/RolesPage/RolesPage.component.tsx | 6 +--- .../TeamsAndUsersPage.component.tsx | 32 ++++++----------- .../src/pages/UserListPage/UserListPage.tsx | 6 +--- .../resources/ui/src/pages/teams/index.tsx | 12 +++---- 14 files changed, 64 insertions(+), 92 deletions(-) diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/dashboardAPI.ts b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/dashboardAPI.ts index 1336e906cc3..9d508c86957 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/dashboardAPI.ts +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/dashboardAPI.ts @@ -15,7 +15,7 @@ import { AxiosResponse } from 'axios'; import { isNil } from 'lodash'; import { Dashboard } from '../generated/entity/data/dashboard'; import { getURLWithQueryFields } from '../utils/APIUtils'; -import APIClient from './index'; +import APIClient, { AxiosClientWithError } from './index'; export const getDashboardVersions: Function = ( id: string @@ -63,7 +63,7 @@ export const getAllDashboards = ( `${searchParams.toString()}${paging ? `&${paging}` : ''}` ); - return APIClient.get(url); + return AxiosClientWithError.get(url); }; export const getDashboardDetails: Function = ( diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/index.js b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/index.js index 97dc96f56bf..9a89dc3fc00 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/index.js +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/index.js @@ -12,9 +12,33 @@ */ import axios from 'axios'; +import jsonData from '../jsons/en'; +import { showErrorToast } from '../utils/ToastUtils'; + +const baseURL = '/api/v1'; const axiosClient = axios.create({ - baseURL: '/api/v1', + baseURL, }); +export const AxiosClientWithError = axios.create({ + baseURL, +}); + +AxiosClientWithError.interceptors.response.use( + (response) => { + if (response.data) { + return Promise.resolve(response); + } else { + throw null; + } + }, + (error) => { + showErrorToast( + error, + jsonData['api-error-messages']['unexpected-server-response'] + ); + } +); + export default axiosClient; diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/pipelineAPI.ts b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/pipelineAPI.ts index 5685a18f1c6..5f0e5d87435 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/pipelineAPI.ts +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/pipelineAPI.ts @@ -15,7 +15,7 @@ import { AxiosResponse } from 'axios'; import { isNil } from 'lodash'; import { Pipeline } from '../generated/entity/data/pipeline'; import { getURLWithQueryFields } from '../utils/APIUtils'; -import APIClient from './index'; +import APIClient, { AxiosClientWithError } from './index'; export const getPipelineVersions: Function = ( id: string @@ -63,7 +63,7 @@ export const getAllPipelines = ( `${searchParams.toString()}${paging ? `&${paging}` : ''}` ); - return APIClient.get(url); + return AxiosClientWithError.get(url); }; export const getPipelineDetails: Function = ( diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/tableAPI.ts b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/tableAPI.ts index 4bec0ba708b..2544d61e9bf 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/tableAPI.ts +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/tableAPI.ts @@ -19,7 +19,7 @@ import { CreateTableTest } from '../generated/api/tests/createTableTest'; import { ColumnTestType } from '../generated/entity/data/table'; import { TableTestType } from '../generated/tests/tableTest'; import { getURLWithQueryFields } from '../utils/APIUtils'; -import APIClient from './index'; +import APIClient, { AxiosClientWithError } from './index'; export const getTableDetails: Function = ( id: string, @@ -76,7 +76,7 @@ export const getAllTables = ( searchParams.toString() ); - return APIClient.get(url); + return AxiosClientWithError.get(url); }; export const getDatabaseTables: Function = ( diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/teamsAPI.ts b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/teamsAPI.ts index e3dd1072d06..5561cbbd22b 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/teamsAPI.ts +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/teamsAPI.ts @@ -14,7 +14,7 @@ import { AxiosResponse } from 'axios'; import { Team } from 'Models'; import { getURLWithQueryFields } from '../utils/APIUtils'; -import APIClient from './index'; +import APIClient, { AxiosClientWithError } from './index'; export const getTeams = ( arrQueryFields?: string | string[], @@ -22,7 +22,9 @@ export const getTeams = ( ): Promise => { const url = getURLWithQueryFields('/teams', arrQueryFields); - return APIClient.get(`${url}${arrQueryFields ? '&' : '?'}limit=${limit}`); + return AxiosClientWithError.get( + `${url}${arrQueryFields ? '&' : '?'}limit=${limit}` + ); }; export const getTeamByName: Function = ( diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/topicsAPI.ts b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/topicsAPI.ts index 34871c5c80a..3089f199121 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/topicsAPI.ts +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/topicsAPI.ts @@ -15,7 +15,7 @@ import { AxiosResponse } from 'axios'; import { isNil } from 'lodash'; import { Topic } from 'Models'; import { getURLWithQueryFields } from '../utils/APIUtils'; -import APIClient from './index'; +import APIClient, { AxiosClientWithError } from './index'; export const getTopicVersions: Function = ( id: string @@ -63,7 +63,7 @@ export const getAllTopics = ( `${searchParams.toString()}${paging ? `&${paging}` : ''}` ); - return APIClient.get(url); + return AxiosClientWithError.get(url); }; export const getTopicDetails: Function = ( diff --git a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/userAPI.ts b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/userAPI.ts index 18d5d364489..c6f7ac49c54 100644 --- a/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/userAPI.ts +++ b/openmetadata-ui/src/main/resources/ui/src/axiosAPIs/userAPI.ts @@ -19,7 +19,7 @@ import { SearchIndex } from '../enums/search.enum'; import { CreateUser } from '../generated/api/teams/createUser'; import { User } from '../generated/entity/teams/user'; import { getURLWithQueryFields } from '../utils/APIUtils'; -import APIClient from './index'; +import APIClient, { AxiosClientWithError } from './index'; export const getUsers = ( arrQueryFields?: string, @@ -41,7 +41,7 @@ export const getUsers = ( ? `${arrQueryFields?.length || qParam ? '&' : '?'}limit=${limit}` : ''); - return APIClient.get(url); + return AxiosClientWithError.get(url); }; export const updateUserDetail = ( diff --git a/openmetadata-ui/src/main/resources/ui/src/components/Users/Users.component.tsx b/openmetadata-ui/src/main/resources/ui/src/components/Users/Users.component.tsx index 68aa9c8a5ad..7af0d47cd88 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/Users/Users.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/Users/Users.component.tsx @@ -97,11 +97,7 @@ const Users = ({ const fetchTeams = () => { getTeams(['users']) .then((res: AxiosResponse) => { - if (res.data) { - setTeams(res.data.data); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTeams(res.data.data); }) .catch((err: AxiosError) => { showErrorToast( diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/CreateUserPage/CreateUserPage.component.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/CreateUserPage/CreateUserPage.component.tsx index e38da50e5a1..4f1e7952863 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/CreateUserPage/CreateUserPage.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/CreateUserPage/CreateUserPage.component.tsx @@ -43,11 +43,7 @@ const CreateUserPage = () => { const fetchTeams = () => { getTeams('defaultRoles') .then((res: AxiosResponse) => { - if (res.data) { - setTeams(res.data.data); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTeams(res.data.data); }) .catch((err: AxiosError) => { showErrorToast( diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx index a08f862027e..2eafb7430a2 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx @@ -96,11 +96,7 @@ const MyDataPage = () => { // limit=0 will fetch empty data list with total count getAllTables('', 0) .then((res) => { - if (res.data) { - setTableCount(res.data.paging.total); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTableCount(res.data.paging.total); }) .catch((err: AxiosError) => { showErrorToast( @@ -113,11 +109,7 @@ const MyDataPage = () => { // limit=0 will fetch empty data list with total count getAllTopics('', '', 0) .then((res) => { - if (res.data) { - setTopicCount(res.data.paging.total); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTopicCount(res.data.paging.total); }) .catch((err: AxiosError) => { showErrorToast( @@ -130,11 +122,7 @@ const MyDataPage = () => { // limit=0 will fetch empty data list with total count getAllPipelines('', '', 0) .then((res) => { - if (res.data) { - setPipelineCount(res.data.paging.total); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setPipelineCount(res.data.paging.total); }) .catch((err: AxiosError) => { showErrorToast( @@ -147,11 +135,7 @@ const MyDataPage = () => { // limit=0 will fetch empty data list with total count getAllDashboards('', '', 0) .then((res) => { - if (res.data) { - setDashboardCount(res.data.paging.total); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setDashboardCount(res.data.paging.total); }) .catch((err: AxiosError) => { showErrorToast( @@ -165,11 +149,7 @@ const MyDataPage = () => { const fetchTeamsAndUsersCount = () => { getUsers('', 0) .then((res) => { - if (res.data) { - setUserCount(res.data.paging.total); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setUserCount(res.data.paging.total); }) .catch((err: AxiosError) => { showErrorToast( @@ -181,11 +161,7 @@ const MyDataPage = () => { getTeams('', 0) .then((res) => { - if (res.data) { - setTeamCount(res.data.paging.total); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTeamCount(res.data.paging.total); }) .catch((err: AxiosError) => { showErrorToast( diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/RolesPage/RolesPage.component.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/RolesPage/RolesPage.component.tsx index c51ed76ff75..5edccda95a2 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/RolesPage/RolesPage.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/RolesPage/RolesPage.component.tsx @@ -216,11 +216,7 @@ const RolesPage = () => { const fetchTeams = () => { getTeams('defaultRoles') .then((res: AxiosResponse) => { - if (res.data) { - setTeamList(res.data.data); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTeamList(res.data.data); }) .catch((err: AxiosError) => { showErrorToast( diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/TeamsAndUsersPage/TeamsAndUsersPage.component.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/TeamsAndUsersPage/TeamsAndUsersPage.component.tsx index 14417acdb7b..c815126f633 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/TeamsAndUsersPage/TeamsAndUsersPage.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/TeamsAndUsersPage/TeamsAndUsersPage.component.tsx @@ -206,10 +206,8 @@ const TeamsAndUsersPage = () => { setIsTeamMemberLoading(true); getUsers('', PAGE_SIZE_MEDIUM, { team, ...pagin }) .then((res: AxiosResponse) => { - if (res.data) { - setCurrentTeamUsers(res.data.data); - setTeamUserPagin(res.data.paging); - } + setCurrentTeamUsers(res.data.data); + setTeamUserPagin(res.data.paging); }) .catch(() => { setCurrentTeamUsers([]); @@ -224,17 +222,13 @@ const TeamsAndUsersPage = () => { const fetchTeams = () => { getTeams(['users', 'owns', 'defaultRoles', 'owner']) .then((res: AxiosResponse) => { - if (res.data) { - if (!teamAndUser && res.data.data.length > 0) { - getCurrentTeamUsers(res.data.data[0].name); - setCurrentTeam(res.data.data[0]); - setIsRightPannelLoading(false); - } - setTeams(res.data.data); - AppState.updateUserTeam(res.data.data); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; + if (!teamAndUser && res.data.data.length > 0) { + getCurrentTeamUsers(res.data.data[0].name); + setCurrentTeam(res.data.data[0]); + setIsRightPannelLoading(false); } + setTeams(res.data.data); + AppState.updateUserTeam(res.data.data); }) .catch((err: AxiosError) => { const errMsg = getErrorText( @@ -255,13 +249,9 @@ const TeamsAndUsersPage = () => { return new Promise((resolve, reject) => { getUsers('profile,teams,roles', API_RES_MAX_SIZE) .then((res: AxiosResponse) => { - if (res.data) { - const resUsers = res.data.data; - setUserList(resUsers); - resolve(); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + const resUsers = res.data.data; + setUserList(resUsers); + resolve(); }) .catch((err: AxiosError) => { const errMsg = getErrorText( diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/UserListPage/UserListPage.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/UserListPage/UserListPage.tsx index cf58290a020..26f5aecc919 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/UserListPage/UserListPage.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/UserListPage/UserListPage.tsx @@ -41,11 +41,7 @@ const UserListPage = () => { setIsLoading(true); getTeams(['users']) .then((res: AxiosResponse) => { - if (res.data) { - setTeams(res.data.data); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; - } + setTeams(res.data.data); }) .catch((err: AxiosError) => { showErrorToast( diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/teams/index.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/teams/index.tsx index 85cf489cbc0..4f3e7cf1ec5 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/teams/index.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/teams/index.tsx @@ -129,15 +129,11 @@ const TeamsPage = () => { setIsLoading(true); getTeams(['users', 'owns', 'defaultRoles', 'owner']) .then((res: AxiosResponse) => { - if (res.data) { - if (!team) { - setCurrentTeam(res.data.data[0]); - } - setTeams(res.data.data); - AppState.updateUserTeam(res.data.data); - } else { - throw jsonData['api-error-messages']['unexpected-server-response']; + if (!team) { + setCurrentTeam(res.data.data[0]); } + setTeams(res.data.data); + AppState.updateUserTeam(res.data.data); }) .catch((err: AxiosError) => { const errMsg = getErrorText(