Skip to content

fix(vtex): handle 400 errors in pageTypesFromUrl gracefully#1566

Open
igoramf wants to merge 7 commits intomainfrom
fix/vtex-intelligent-search-pagetype-400-error
Open

fix(vtex): handle 400 errors in pageTypesFromUrl gracefully#1566
igoramf wants to merge 7 commits intomainfrom
fix/vtex-intelligent-search-pagetype-400-error

Conversation

@igoramf
Copy link
Copy Markdown
Contributor

@igoramf igoramf commented Apr 1, 2026

Summary

  • pageTypesFromUrl calls the VTEX Catalog API (/api/catalog_system/pub/portal/pagetype/:term) for each URL segment to determine page type
  • When the URL contains invalid segments (e.g. bot probes like /%252f/backend/.env), the API returns HTTP 400 with "Invalid parameter 'term'"
  • This error was propagating through Promise.all, crashing section rendering and generating noisy error logs

Fix

Added .catch(() => ({ pageType: "NotFound" })) to the API call so that:

  1. Failed segments return { pageType: "NotFound" } instead of throwing
  2. getValidTypesFromPageTypes already filters out NotFound (mapped as null in PAGE_TYPE_TO_MAP_PARAM)
  3. The loader ends up with empty pageTypes, no valid facets, and returns null cleanly

Test plan

  • Visit a valid category URL — should work as before
  • Visit a URL with an invalid path like /%252f/backend/.env — should return null from loader with no error thrown

🤖 Generated with Claude Code


Summary by cubic

Gracefully handle HTTP 400s from the VTEX pagetype endpoint in pageTypesFromUrl by returning a full PageType with pageType: 'NotFound' so bad URL segments (e.g., bot probes) don’t crash sections; non-400 errors still bubble up. Also switch utils/http.ts to use DecoRequestInit from our fetch.ts instead of RequestInit from @deco/deco (removed in 1.188.0).

Written for commit fe1d2ac. Summary will update on new commits.

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for page-type lookups: individual client errors are now treated as "NotFound" and no longer cause the overall operation to fail, allowing the app to continue and display available content.
  • Chores
    • Internal type alignment for request initialization to standardize internal request typing with no visible runtime change.

When VTEX Catalog API returns a 400 for invalid URL segments (e.g. bot
probes like /%252f/backend/.env), the error was propagating and breaking
section rendering. Return { pageType: "NotFound" } on failure so
getValidTypesFromPageTypes filters it out and the loader returns null
cleanly.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Tagging Options

Should a new tag be published when this PR is merged?

  • 👍 for Patch 0.142.2 update
  • 🎉 for Minor 0.143.0 update
  • 🚀 for Major 1.0.0 update

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: af36b396-2d5f-484b-9ee1-76ef578179a0

📥 Commits

Reviewing files that changed from the base of the PR and between f84771d and fe1d2ac.

📒 Files selected for processing (2)
  • utils/http.ts
  • vtex/utils/intelligentSearch.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • vtex/utils/intelligentSearch.ts

📝 Walkthrough

Walkthrough

Adds per-segment error handling in pageTypesFromUrl to convert fetch/JSON errors with status === 400 into { pageType: "NotFound" } results, and changes utils/http.ts to re-export DecoRequestInit from ./fetch.ts under the local alias RequestInit for typing.

Changes

Cohort / File(s) Summary
Intelligent search error handling
vtex/utils/intelligentSearch.ts
Adds try/catch around each segment's vcsDeprecated["GET /api/catalog_system/pub/portal/pagetype/:term"] call and res.json(); status === 400 errors resolve to a PageType-shaped object with pageType: "NotFound", other errors are rethrown.
HTTP typing alias update
utils/http.ts, ./fetch.ts
Replaces import of RequestInit from @deco/deco by re-exporting DecoRequestInit from ./fetch.ts under the local alias RequestInit, adjusting the source type used by TypedRequestInit<T> while leaving runtime imports unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • guitavano

Poem

🐰 I danced through lines of code so bright,

A 400 stumble turned to light,
Now missing pages softly say,
"NotFound" and hop along their way,
🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: graceful handling of 400 errors in pageTypesFromUrl, which is the primary focus of the PR.
Description check ✅ Passed The description covers what the issue is and how it's fixed, but is missing the required Issue Link section and the optional Loom Video and Demonstration Link sections from the template.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/vtex-intelligent-search-pagetype-400-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@vtex/utils/intelligentSearch.ts`:
- Around line 151-153: Run the code formatter (deno fmt) on the changed block
containing the promise chain that uses STALE and the .then(...).catch(...) chain
so CI passes; specifically format the fragment with the call that ends with "),
STALE).then((res) => res.json()).catch(() => ({ pageType: "NotFound" }))" inside
the intelligent search utility so spacing/line breaks match project style (you
can run `deno fmt` or your editor's formatter to fix indentation and line breaks
for the function handling this promise).
- Around line 149-152: The code maps all exceptions to {pageType: "NotFound"} in
the vcsDeprecated["GET /api/catalog_system/pub/portal/pagetype/:term"] call,
which hides non-400 errors; change the promise handling to only treat HTTP 400
as NotFound and let other failures surface. Concretely, replace the current
.then(res => res.json()).catch(() => ({ pageType: "NotFound" })) chain with
logic that checks res.status: if res.status === 400 return { pageType:
"NotFound" }, else if (!res.ok) throw (or return a rejected promise with) the
response/error so network/5xx/auth errors propagate, and only call res.json()
for successful responses; remove the blanket catch that swallows all errors.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d8e5664c-150d-4d3d-9c6d-b30f0d33d97e

📥 Commits

Reviewing files that changed from the base of the PR and between 0b95d39 and ad77c9b.

📒 Files selected for processing (1)
  • vtex/utils/intelligentSearch.ts

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-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.

No issues found across 1 file

igoramf and others added 6 commits April 1, 2026 05:38
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…8.0)

RequestInit is a global Deno/Web API type, no import needed.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…rom fetch.ts

@deco/deco removed RequestInit from its exports in 1.188.0.
DecoRequestInit from fetch.ts is the same type (RequestInit & { deco? }).

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
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