Skip to content

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

Improves robustness of JSON-RPC data fetching by properly surfacing HTTP failures.

  • Add response.ok check in fetchData before JSON parsing
  • On non-2xx, return { data: null, error: Error(status) } instead of parsing the body
  • In catch, return the original Error (or wrap non-Error) instead of true
  • No behavioral changes to fetchMultipleData or sorting; impact localized to error handling in fetchData

Written by Cursor Bugbot for commit b7b732d. 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

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