Skip to content

fix: handle 404 errors gracefully in product API calls#764

Merged
philipbrembeck merged 1 commit intostagingfrom
feat/InitRedis
Jun 27, 2025
Merged

fix: handle 404 errors gracefully in product API calls#764
philipbrembeck merged 1 commit intostagingfrom
feat/InitRedis

Conversation

@philipbrembeck
Copy link
Copy Markdown
Contributor

@philipbrembeck philipbrembeck commented Jun 27, 2025

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:

  • Gracefully return fallback values on 404 responses from external API calls in ProductService instead of propagating errors

Enhancements:

  • Unwrap axios responses to return only response data in cached fetchers for OpenFoodFacts, PETA, OpenEANDB, and grades APIs
  • Adjust property accesses to match simplified response shapes
  • Add try-catch wrappers around external API calls to intercept 404s and fallback appropriately
  • Enhance controller error handling by catching NotFoundException and returning a consistent 404 JSON response

Documentation:

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Jun 27, 2025

Reviewer's Guide

This 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 handling

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Simplified response object property usage and data extraction
  • Removed redundant .data access and updated property paths for OpenFoodFacts and PETA responses
  • Parsed raw OpenEANDB response instead of nested data
  • Standardized returning of response.data across service methods
src/product/product.service.ts
Introduced 404 error handling in fetchWithCache wrappers
  • Wrapped grade API call to catch 404 and return “404” string
  • Caught 404 from OpenFoodFacts GET and returned default {status:0, product:null} structure
  • Re-threw non-404 errors
src/product/product.service.ts
Enhanced controller error handling
  • Distinguished NotFoundException to return 404 with JSON error
  • Handled other exceptions with a 500 internal server error response
src/product/product.controller.ts
Updated API documentation and Swagger setup
  • Added local server URL to OpenAPI.yaml
  • Registered localhost entry in SwaggerModule configuration
OpenAPI.yaml
src/main.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@philipbrembeck philipbrembeck merged commit f037160 into staging Jun 27, 2025
4 checks passed
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (code-quality): Prefer object destructuring when accessing and using properties. (use-object-destructuring)

Suggested change
const product = openFoodFactsResponse.product;
const {product} = openFoodFactsResponse;


ExplanationObject destructuring can often remove an unnecessary temporary reference, as well as making your code more succinct.

From the Airbnb Javascript Style Guide

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.

1 participant