Skip to content

fix: throw better error when ensrainbow fails to serve json#1713

Open
shrugs wants to merge 7 commits intomainfrom
fix/ensrainbow-not-found-handling
Open

fix: throw better error when ensrainbow fails to serve json#1713
shrugs wants to merge 7 commits intomainfrom
fix/ensrainbow-not-found-handling

Conversation

@shrugs
Copy link
Collaborator

@shrugs shrugs commented Mar 4, 2026

Summary

  • remove testing of error responses for config endpoint, it doesn't return an error response
  • wrap response.json() in the error path of EnsRainbowApiClient.config() with try-catch so non-JSON error responses (e.g. plain text 404 Not Found) fall through to the statusText-based fallback message instead of throwing a SyntaxError
  • add unit test for the non-JSON error body case

Why

  • when ENSRainbow returns a non-JSON error response (e.g. plain text 404 Not Found), response.json() throws a SyntaxError that propagates unhandled through the ENSIndexer's /api/config endpoint, causing the EnsDbWriterWorker to crash
    and take down the entire process
  • ran into this in local dev with old ENSRainbow instance

Testing

  • existing and new unit tests pass (pnpm test in packages/ensrainbow-sdk, 25 tests)
  • reproduced locally by running pnpm dev against a local ENSRainbow that returns 404 Not Found for /v1/config

Checklist

  • This PR is low-risk and safe to review quickly

@shrugs shrugs requested a review from a team as a code owner March 4, 2026 00:44
Copilot AI review requested due to automatic review settings March 4, 2026 00:44
@vercel
Copy link
Contributor

vercel bot commented Mar 4, 2026

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

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
admin.ensnode.io Skipped Skipped Mar 4, 2026 4:54pm
ensnode.io Skipped Skipped Mar 4, 2026 4:54pm
ensrainbow.io Skipped Skipped Mar 4, 2026 4:54pm

@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

⚠️ No Changeset found

Latest commit: 94f51a5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

📝 Walkthrough

Walkthrough

The changes simplify HTTP error handling in the EnsRainbow SDK by removing JSON error payload parsing in the config() method and updating corresponding test cases. Additionally, unused imports are removed from the version-info module.

Changes

Cohort / File(s) Summary
Error Handling Refactoring
packages/ensrainbow-sdk/src/client.ts, packages/ensrainbow-sdk/src/client.test.ts
Simplified error handling to use HTTP statusText directly instead of parsing JSON error payloads. Test cases updated to reflect new error handling behavior.
Unused Import Cleanup
apps/ensindexer/src/lib/version-info.ts
Removed three unused imports: prettifyError, type imports ENSIndexerVersionInfo and SerializedENSIndexerVersionInfo, and makeENSIndexerVersionInfoSchema.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop and a bound, the errors now clear,
No JSON to parse, just statusText here!
And imports unused? Away they do go,
Cleaner code blooms where the test rabbits flow! 🌈

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: improving error handling in ensrainbow by throwing better errors when JSON parsing fails.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The pull request description follows the template structure with all required sections completed: Summary (3 bullets covering changes), Why (motivation with context), Testing (unit tests and local reproduction), and Pre-Review Checklist (marked as low-risk).

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/ensrainbow-not-found-handling

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.

@vercel vercel bot temporarily deployed to Preview – admin.ensnode.io March 4, 2026 00:46 Inactive
@vercel vercel bot temporarily deployed to Preview – ensrainbow.io March 4, 2026 00:46 Inactive
@vercel vercel bot temporarily deployed to Preview – ensnode.io March 4, 2026 00:46 Inactive
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 4, 2026

Greptile Summary

This PR fixes a crash in EnsDbWriterWorker caused by an unhandled SyntaxError when ENSRainbow returns a non-JSON error response (e.g. plain-text 404 Not Found) to the EnsRainbowApiClient.config() method. The fix wraps response.json() in a try-catch so invalid JSON bodies fall through to a statusText-based fallback error message. A targeted unit test covers the new code path, and the happy path remains unchanged.

Confidence Score: 5/5

  • This PR is safe to merge — it is a narrow, well-tested defensive fix with no behavior change on the happy path.
  • The change is minimal and surgical: only the error branch of one method is touched, the happy path is completely unaffected, and existing test cases plus one new case all cover the relevant scenarios. The package.json change is a low-risk tooling fix.
  • No files require special attention

Last reviewed commit: 9268bbf

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves resilience of the ENSRainbow SDK client when /v1/config returns a non-JSON error response, preventing JSON parse failures from bubbling up unexpectedly and adding test coverage for that scenario.

Changes:

  • Wrap response.json() in EnsRainbowApiClient.config() error handling with a try/catch to fall back to a statusText-based message when the error body isn’t valid JSON.
  • Add a unit test covering the non-JSON error body case.
  • Adjust apps/ensindexer typecheck script to run tsc via pnpm exec.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/ensrainbow-sdk/src/client.ts Makes config-fetch error handling robust to invalid/non-JSON error bodies.
packages/ensrainbow-sdk/src/client.test.ts Adds a regression test ensuring fallback messaging on non-JSON error bodies.
apps/ensindexer/package.json Updates the typecheck script invocation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@vercel vercel bot temporarily deployed to Preview – admin.ensnode.io March 4, 2026 15:59 Inactive
@vercel vercel bot temporarily deployed to Preview – ensrainbow.io March 4, 2026 15:59 Inactive
@vercel vercel bot temporarily deployed to Preview – ensnode.io March 4, 2026 15:59 Inactive
Copilot AI review requested due to automatic review settings March 4, 2026 16:01
@vercel vercel bot temporarily deployed to Preview – admin.ensnode.io March 4, 2026 16:01 Inactive
@vercel vercel bot temporarily deployed to Preview – ensnode.io March 4, 2026 16:01 Inactive
@vercel vercel bot temporarily deployed to Preview – ensrainbow.io March 4, 2026 16:01 Inactive
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
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.

♻️ Duplicate comments (1)
packages/ensrainbow-sdk/src/client.ts (1)

403-407: ⚠️ Potential issue | 🔴 Critical

Fix the type error and narrow the parse-failure fallback.

Line 405 fails typecheck (json() resolves to unknown here), and Line 406 currently swallows all parse/runtime errors instead of only the non-JSON case. This is a release-blocking correctness issue (CI already failing).

Proposed fix
-      const errorMessage = await response
-        .json()
-        .then((data: { error?: string }) => data.error)
-        .catch(() => undefined);
+      let errorMessage: string | undefined;
+      try {
+        const data = (await response.json()) as { error?: unknown };
+        if (typeof data.error === "string") {
+          errorMessage = data.error;
+        }
+      } catch (error) {
+        if (!(error instanceof SyntaxError)) {
+          throw error;
+        }
+      }
       throw new Error(errorMessage ?? `Failed to fetch ENSRainbow config: ${response.statusText}`);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/ensrainbow-sdk/src/client.ts` around lines 403 - 407, The current
use of response.json() assumes a typed result and indiscriminately catches all
errors; change to explicitly await response.json(), narrow-guard the parsed
value (ensure it's a non-null object and has a string 'error' property) to
assign errorMessage, and only swallow JSON parse errors (SyntaxError) while
rethrowing other exceptions. Concretely, replace the promise chain with a
try/await block that does: const parsed = await response.json(); if (parsed &&
typeof parsed === 'object' && 'error' in parsed && typeof (parsed as any).error
=== 'string') errorMessage = (parsed as any).error; catch (e) { if (!(e
instanceof SyntaxError)) throw e; } then throw new Error(errorMessage ?? `Failed
to fetch ENSRainbow config: ${response.statusText}`), updating the code around
response, errorMessage to use these checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@packages/ensrainbow-sdk/src/client.ts`:
- Around line 403-407: The current use of response.json() assumes a typed result
and indiscriminately catches all errors; change to explicitly await
response.json(), narrow-guard the parsed value (ensure it's a non-null object
and has a string 'error' property) to assign errorMessage, and only swallow JSON
parse errors (SyntaxError) while rethrowing other exceptions. Concretely,
replace the promise chain with a try/await block that does: const parsed = await
response.json(); if (parsed && typeof parsed === 'object' && 'error' in parsed
&& typeof (parsed as any).error === 'string') errorMessage = (parsed as
any).error; catch (e) { if (!(e instanceof SyntaxError)) throw e; } then throw
new Error(errorMessage ?? `Failed to fetch ENSRainbow config:
${response.statusText}`), updating the code around response, errorMessage to use
these checks.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 75a91951-6e1c-4f9d-8d78-486e184a8163

📥 Commits

Reviewing files that changed from the base of the PR and between 9268bbf and 7c8f51d.

📒 Files selected for processing (2)
  • apps/ensindexer/package.json
  • packages/ensrainbow-sdk/src/client.ts

@vercel vercel bot temporarily deployed to Preview – admin.ensnode.io March 4, 2026 16:12 Inactive
@vercel vercel bot temporarily deployed to Preview – ensrainbow.io March 4, 2026 16:12 Inactive
@vercel vercel bot temporarily deployed to Preview – ensnode.io March 4, 2026 16:12 Inactive
Copilot AI review requested due to automatic review settings March 4, 2026 16:54
@vercel vercel bot temporarily deployed to Preview – ensnode.io March 4, 2026 16:54 Inactive
@vercel vercel bot temporarily deployed to Preview – ensrainbow.io March 4, 2026 16:54 Inactive
@vercel vercel bot temporarily deployed to Preview – admin.ensnode.io March 4, 2026 16:54 Inactive
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
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 `@packages/ensrainbow-sdk/src/client.test.ts`:
- Around line 320-324: Add a new unit test next to the existing non-JSON
fallback test in packages/ensrainbow-sdk/src/client.test.ts that verifies JSON
error-body precedence: mock the fetch used by the tests
(mockFetch.mockResolvedValueOnce) to return an object with ok: false and a json
method that resolves to { error: "Your error message" } (and statusText if
desired), then call client.config() and assert the promise rejects with an error
containing that JSON error string (e.g., await
expect(client.config()).rejects.toThrow(/Your error message/)); mirror the
structure and naming of the existing test so it's clear this is the
JSON-precedence companion.

In `@packages/ensrainbow-sdk/src/client.ts`:
- Around line 402-404: The current error throw uses only response.statusText and
discards any JSON error payload; update the error path that throws for non-ok
responses to read the response body first and include its details: attempt to
parse await response.json() (falling back to await response.text() if parsing
fails), extract a useful message (e.g., body.message || body.error || the raw
text or JSON stringified body), and then throw a new Error that includes
response.status, response.statusText, and the extracted error details; reference
the existing response variable and the throw that currently reads `Failed to
fetch ENSRainbow config: ${response.statusText}` and replace it with the richer
message as described.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: c6357201-19b3-4a2f-9e02-44b5e92ccd29

📥 Commits

Reviewing files that changed from the base of the PR and between 7c8f51d and 94f51a5.

📒 Files selected for processing (3)
  • apps/ensindexer/src/lib/version-info.ts
  • packages/ensrainbow-sdk/src/client.test.ts
  • packages/ensrainbow-sdk/src/client.ts
💤 Files with no reviewable changes (1)
  • apps/ensindexer/src/lib/version-info.ts

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