fix: handle 404 errors gracefully in product API calls#764
Merged
philipbrembeck merged 1 commit intostagingfrom Jun 27, 2025
Merged
fix: handle 404 errors gracefully in product API calls#764philipbrembeck merged 1 commit intostagingfrom
philipbrembeck merged 1 commit intostagingfrom
Conversation
|
|
Reviewer's GuideThis PR refactors product API integrations by simplifying response parsing, introducing graceful 404 handling in cached HTTP calls, standardizing data extraction in cache wrappers, enhancing controller error discrimination, and updating Swagger config with local server details. Sequence diagram for product API call with graceful 404 handlingsequenceDiagram
actor User
participant Controller as ProductController
participant Service as ProductService
participant ExternalAPI as External Product APIs
User->>Controller: GET /product/:barcode
Controller->>Service: getProductByBarcode(barcode)
Service->>ExternalAPI: fetch product data (OpenFoodFacts, etc.)
alt API returns 404
ExternalAPI-->>Service: { status: 0, product: null } or "404"
Service-->>Controller: null or error
Controller-->>User: 404 Not Found JSON
else API returns data
ExternalAPI-->>Service: product data
Service-->>Controller: product result
Controller-->>User: 200 OK JSON
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @philipbrembeck - I've reviewed your changes - here's some feedback:
- The repeated try/catch logic for fetching and unwrapping response.data in multiple fetchWithCache calls could be extracted into a reusable helper to reduce duplication.
- The service currently returns mixed 404 payloads (raw strings vs objects) which may not align with the controller’s NotFoundException handling—consider standardizing the error response shape.
- You’re mixing axios calls and NestJS HttpService throughout the service; consolidating on a single HTTP client could simplify error handling and testing.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The repeated try/catch logic for fetching and unwrapping response.data in multiple fetchWithCache calls could be extracted into a reusable helper to reduce duplication.
- The service currently returns mixed 404 payloads (raw strings vs objects) which may not align with the controller’s NotFoundException handling—consider standardizing the error response shape.
- You’re mixing axios calls and NestJS HttpService throughout the service; consolidating on a single HTTP client could simplify error handling and testing.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if (openFoodFactsResponse.data.status === 1) { | ||
| const product = openFoodFactsResponse.data.product; | ||
| if (openFoodFactsResponse.status === 1) { | ||
| const product = openFoodFactsResponse.product; |
There was a problem hiding this comment.
suggestion (code-quality): Prefer object destructuring when accessing and using properties. (use-object-destructuring)
Suggested change
| const product = openFoodFactsResponse.product; | |
| const {product} = openFoodFactsResponse; |
Explanation
Object destructuring can often remove an unnecessary temporary reference, as well as making your code more succinct.From the Airbnb Javascript Style Guide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Handle 404 errors gracefully across all external product data fetchers, streamline HTTP response handling, and update error mapping and API documentation.
Bug Fixes:
Enhancements:
Documentation: