Skip to content
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

Conversation

nidhigarg-bmw
Copy link
Contributor

Description

Backend API Error handling according to status code

Why

UI Improvementts

Issue

n/a

Checklist

Please delete options that are not relevant.

  • I have followed the contributing guidelines
  • I have performed IP checks for added or updated 3rd party libraries
  • I have created and linked IP issues or requested their creation by a committer
  • I have performed a self-review of my own code
  • I have successfully tested my changes locally
  • I have added tests that prove my changes work
  • I have checked that new and existing tests pass locally with my changes
  • I have commented my code, particularly in hard-to-understand areas

@nidhigarg-bmw nidhigarg-bmw requested review from oyo, lavanya-bmw and manojava-gk and removed request for oyo November 7, 2023 08:00
Copy link

sonarqubecloud bot commented Nov 7, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Contributor

@manojava-gk manojava-gk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine to me

Copy link
Contributor

@oyo oyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is becoming too complicated now - convert to RTK Query and save a lot of effort

@@ -101,10 +101,10 @@ export const VerifyRegistration = ({
}

const hasRoles = () => {
return true
return registrationData.companyRoles.length > 0
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also remove the { return * }

const hasRoles = () => registrationData.companyRoles.length > 0

}
const hasDocuments = () => {
return documents && documents.length > 0 ? true : false
return documents && documents.length > 0
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write as

const hasDocuments = () => documents && documents.length > 0

default:
return t('ErrorMessage.defaultReload')
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too complicated. write simply as:

const getErrorMessage = (errorCode: number) => t(\ErrorMessage.${errorCode}`

and then add descriptions in the jsons like

"ErrorMessage": {
"400": "...",
"401": "...",
etc.

@@ -76,7 +76,7 @@ export const CompanyDataCax = ({
const applicationId = obj['applicationId']

useEffect(() => {
dispatch(getCompanyDetailsWithAddress(applicationId))
dispatch(getCompanyDetailsWithAddress({applicationId, dispatch}))
Copy link
Contributor

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

@@ -61,13 +62,15 @@ const updateStatus = createAsyncThunk(

const getCompanyDetailsWithAddress = createAsyncThunk(
`${name}/companyDetailsWithAddress/get`,
async (applicationId: string) => {
async ({applicationId, dispatch}: {applicationId: string, dispatch: Dispatch}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

export const statusCodeSelector = (state: RootState): number =>
state.statuscode.errorCode

export default slice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file because we can handle errors the RTK Query way

return await ApplicationApi.getInstance().getCompanyDetailsWithAddress(
applicationId
)
} catch (error: unknown) {
} catch (error: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use try { ... } catch (error: any) {...} here - instead convert this into a RTK Query apiSlice with built in error handling

@oyo oyo closed this Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants