-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api error): backend API error handling #70
Changes from all commits
3122081
5ffd92d
0da9ccb
22bb6d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ import { | |
fetchId, | ||
updateInvitation, | ||
} from '../state/features/application/actions' | ||
import { statusCodeSelector } from '../state/features/statusCodeError' | ||
interface RegistrationCaxProps { | ||
currentActiveStep: number | ||
} | ||
|
@@ -52,6 +53,28 @@ export const RegistrationCax = ({ | |
const dispatch = useDispatch() | ||
const { status, error } = useSelector(applicationSelector) | ||
|
||
const errorCode = useSelector(statusCodeSelector) | ||
|
||
const getErrorMessage = (errorCode: number) => { | ||
console.log('errorCode', typeof errorCode) | ||
switch (errorCode){ | ||
case 400: | ||
case 404: | ||
return t('ErrorMessage.connectAdministrator') | ||
case 401: | ||
case 403: | ||
return t('ErrorMessage.missingPermissions') | ||
case 409: | ||
return t('ErrorMessage.connectAdministrator') | ||
default: | ||
return t('ErrorMessage.defaultReload') | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too complicated. write simply as:
and then add descriptions in the jsons like
|
||
|
||
useEffect(() => { | ||
errorCode && toast.error(getErrorMessage(errorCode)) | ||
},[errorCode]) | ||
|
||
if (error) { | ||
toast.error(error) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,10 +101,10 @@ export const VerifyRegistration = ({ | |
} | ||
|
||
const hasRoles = () => { | ||
return true | ||
return registrationData.companyRoles.length > 0 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also remove the { return * }
|
||
const hasDocuments = () => { | ||
return documents && documents.length > 0 ? true : false | ||
return documents && documents.length > 0 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. write as
|
||
|
||
return ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,10 @@ | |
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
import { createAsyncThunk } from '@reduxjs/toolkit' | ||
import { Dispatch, createAsyncThunk } from '@reduxjs/toolkit' | ||
import { ApplicationApi } from './api' | ||
import { ApplicationStatus, CompanyDetails, name } from './types' | ||
import { setStatusCode } from '../statusCodeError' | ||
|
||
const fetchId = createAsyncThunk(`${name}/fetchId`, async () => { | ||
try { | ||
|
@@ -61,13 +62,15 @@ const updateStatus = createAsyncThunk( | |
|
||
const getCompanyDetailsWithAddress = createAsyncThunk( | ||
`${name}/companyDetailsWithAddress/get`, | ||
async (applicationId: string) => { | ||
async ({applicationId, dispatch}: {applicationId: string, dispatch: Dispatch}) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as written in the other comment convert this to an RTK Query apiSlice and don't use dispatch inside another redux reducer. We can then handle the error outside of the Redux code as shown in another comment |
||
try { | ||
dispatch(setStatusCode({ errorCode: '' })) | ||
return await ApplicationApi.getInstance().getCompanyDetailsWithAddress( | ||
applicationId | ||
) | ||
} catch (error: unknown) { | ||
} catch (error: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use |
||
console.error('api call error:', error) | ||
dispatch(setStatusCode({ errorCode: error.response.status })) | ||
throw Error( | ||
'No active company application existing. Please contact the administrator.' | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
import { createSlice } from '@reduxjs/toolkit' | ||
import type { RootState } from '../store' | ||
|
||
const name = 'statuscode' | ||
|
||
const initialState = { | ||
errorCode: null, | ||
} | ||
|
||
export const slice = createSlice({ | ||
name, | ||
initialState, | ||
reducers: { | ||
setStatusCode: (state, { payload }) => ({ | ||
...state, | ||
...payload, | ||
}) | ||
}, | ||
}) | ||
|
||
export const { setStatusCode } = slice.actions | ||
|
||
export const statusCodeSelector = (state: RootState): number => | ||
state.statuscode.errorCode | ||
|
||
export default slice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this file because we can handle errors the RTK Query way |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the old way to do api calls - also it is considered bad practice to call dispatch from a Redux action. See answers in https://stackoverflow.com/a/41260990/830067
convert the redux code to the new apiSlice way as has been done in the portal frontend and then use
const { data, error } = useGetCompanyDetailsWithAddressQuery(applicationId)
outside of the useEffect. We can also handle the error here instead of the unnecessary statusCodeError.ts