Skip to content

Add HTTP status check in fetchData to prevent treating error responses as success#2598

Merged
bgravenorst merged 2 commits intoMetaMask:mainfrom
subhamkumarr:fix/missing-http-status-check-fetch
Feb 10, 2026
Merged

Add HTTP status check in fetchData to prevent treating error responses as success#2598
bgravenorst merged 2 commits intoMetaMask:mainfrom
subhamkumarr:fix/missing-http-status-check-fetch

Conversation

@subhamkumarr
Copy link
Contributor

@subhamkumarr subhamkumarr commented Dec 26, 2025

Problem

The fetchData function in src/plugins/plugin-json-rpc.ts was missing a critical HTTP status check before parsing JSON responses. This caused several issues:

  1. Error responses treated as success: When APIs returned 404, 500, or other non-2xx status codes, the error response body was parsed as JSON and returned with error: false
  2. Silent failures: HTTP errors were not properly detected, making debugging difficult
  3. Build-time issues: During Docusaurus builds, if external APIs were unavailable, error responses could be processed as valid data
  4. Incorrect data processing: Error response bodies could be used to generate routes/sidebars incorrectly

Solution

Added proper HTTP status checking:

  • Check response.ok before parsing JSON
  • Return proper Error objects for HTTP errors with status information
  • Improved error handling to distinguish between network errors and HTTP errors
  • Maintain backward compatibility with existing error handling logic

Changes

File: src/plugins/plugin-json-rpc.ts

  • Added if (!response.ok) check before JSON parsing
  • Return Error object with status code for HTTP errors
  • Improved catch block to properly handle Error instances
  • Maintains existing ResponseItem interface compatibility

Before:

async function fetchData(url: string, name: string): Promise<ResponseItem> {
  try {
    const response = await fetch(url, { method: 'GET' })
    const data = await response.json()  // ❌ No status check
    return { name, data, error: false }
  } catch (error) {
    return { name, data: null, error: true }
  }
}

After:

async function fetchData(url: string, name: string): Promise<ResponseItem> {
  try {
    const response = await fetch(url, { method: 'GET' })
    if (!response.ok) {  // ✅ Check HTTP status
      return {
        name,
        data: null,
        error: new Error(`HTTP error! status: ${response.status}`),
      }
    }
    const data = await response.json()
    return { name, data, error: false }
  } catch (error) {
    return {
      name,
      data: null,
      error: error instanceof Error ? error : new Error(String(error)),
    }
  }
}

Note

Low Risk
Small, localized change to fetch error handling that reduces false-success cases; main risk is behavior changes for callers that previously relied on parsing error bodies.

Overview
Tightens error handling in src/plugins/plugin-json-rpc.ts by treating non-2xx fetch responses as failures instead of parsing them as successful JSON payloads.

fetchData now checks response.ok and returns a structured Error (including HTTP status) with data: null, and normalizes thrown values in the catch path to an Error instance to avoid silent/ambiguous failures during sidebar/route generation.

Written by Cursor Bugbot for commit 01df1a9. This will update automatically on new commits. Configure here.

@subhamkumarr subhamkumarr requested review from a team as code owners December 26, 2025 19:56
@vercel
Copy link

vercel bot commented Dec 26, 2025

@subhamkumarr is attempting to deploy a commit to the Consensys Team on Vercel.

A member of the Team first needs to authorize it.

@subhamkumarr subhamkumarr force-pushed the fix/missing-http-status-check-fetch branch from 84888fe to b7b732d Compare December 26, 2025 20:00
@subhamkumarr
Copy link
Contributor Author

@AyushBherwani1998, could you please review this and merge it when you get a chance? Thanks!
Fix #2598

@subhamkumarr
Copy link
Contributor Author

@alexandratran could you please review this and merge it when you get a chance? Thanks!
Fix #2598

Copy link
Contributor

@bgravenorst bgravenorst left a comment

Choose a reason for hiding this comment

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

lgtm

@vercel
Copy link

vercel bot commented Feb 10, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
metamask-docs Ready Ready Preview, Comment Feb 10, 2026 9:21pm

Request Review

@bgravenorst bgravenorst merged commit 5f3339a into MetaMask:main Feb 10, 2026
18 checks passed
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

Comments