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

fix: handle api 404 error to display data properly #1802

Merged
merged 1 commit into from
Oct 26, 2024

Conversation

yunchipang
Copy link
Collaborator

@yunchipang yunchipang commented Oct 24, 2024

Description

Fixes #1801

⛔️ Cause: when populating static props (captures, countries, growers, organizations, wallets) in top.js, if any of the 5 items got a 404 upon API request, the page renders a 404 not found because the field was not properly populated.

🧩 Solution: consolidate all API request functions into src/models/api.js, including error handling. When an API request failed, the function returns an empty array.

📝 Note: For now, API requests to captures and growers return 404, so the below error persists, and the frontend is only displaying countries, organizations, and wallets (please refer to the screenshot in the next section). I believe the investigation should continue on treetracker-query-api in terms of fetching captures and featured growers. I've checked /captures and /growers/featured endpoints, neither of them are working.

image

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Screenshots

Before After
image image

How Has This Been Tested?

  • Cypress integration
  • Cypress component tests
  • Jest unit tests

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@yunchipang yunchipang requested a review from dadiorchen October 24, 2024 23:23
log.error(err.message);
throw err;
log.error('Error in getCaptures:', err.message);
return [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

@yunchipang why you disable the throwing here?

Copy link
Collaborator Author

@yunchipang yunchipang Oct 25, 2024

Choose a reason for hiding this comment

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

because if it throws an error, getCaptures() will not return anything, then captures in static props gets undefined, and the entire top page just shows 404.

for now, to inform the error i'm just logging it. would you like this to be handled in some other way? my purpose is to have some props display on the page even when some other props were not populated properly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure, let's merge your code first, and we can continue, we can back to this if there are some problem.

BTW, I also don't like the try-catch way to handle error, I prefer to use Promise function to do it:

promise
.then(goodHandler)
.catch(badHandler)

@yunchipang yunchipang requested a review from dadiorchen October 25, 2024 16:19
log.error(err.message);
throw err;
log.error('Error in getCaptures:', err.message);
return [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure, let's merge your code first, and we can continue, we can back to this if there are some problem.

BTW, I also don't like the try-catch way to handle error, I prefer to use Promise function to do it:

promise
.then(goodHandler)
.catch(badHandler)

@dadiorchen dadiorchen merged commit c6b429e into Greenstand:v2 Oct 26, 2024
3 checks passed
@dadiorchen
Copy link
Collaborator

@yunchipang thanks for you contribution!

@yunchipang yunchipang deleted the fix/1801-404-error-on-top-page branch October 29, 2024 00:52
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.

2 participants