Skip to content

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

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

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

Conversation

@shrugs
Copy link
Collaborator

@shrugs shrugs commented Mar 4, 2026

Summary

  • 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 does not change runtime behavior or semantics
  • 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 0:46am
ensnode.io Skipped Skipped Mar 4, 2026 0:46am
ensrainbow.io Skipped Skipped Mar 4, 2026 0:46am

@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

⚠️ No Changeset found

Latest commit: 9268bbf

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

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

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

📝 Walkthrough

Walkthrough

The pull request improves error handling in the EnsRainbow SDK's API client to gracefully manage non-JSON error responses by adding a fallback message, adds a test case validating this behavior, and updates the ensindexer build script to invoke TypeScript compiler through pnpm.

Changes

Cohort / File(s) Summary
Build Configuration
apps/ensindexer/package.json
Updated typecheck script to invoke tsc via pnpm: "tsc --noEmit""pnpm exec tsc --noEmit".
Error Handling & Tests
packages/ensrainbow-sdk/src/client.ts, packages/ensrainbow-sdk/src/client.test.ts
Enhanced config() method to defensively parse error response JSON with fallback to statusText-based message on parse failure. New test case validates error handling when JSON parsing fails.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 When errors lurk in JSON's shade,
We catch and parse with careful trade,
But if the JSON breaks apart,
A fallback message saves the heart!
With tests in place and scripts so neat,
Our bundle's error-handling's sweet! 🎀

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: throw better error when ensrainbow fails to serve json' directly describes the main change: improving error handling when ENSRainbow returns non-JSON error responses.
Description check ✅ Passed The description includes all required template sections: Summary (3 bullets covering the changes), Why (explaining the motivation and issue), Testing (detailing test execution and reproduction), and a completed pre-review checklist.
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 (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.

Comment on lines +407 to +408
} catch {
// response body is not valid JSON
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The catch here swallows any error thrown by response.json() (not just invalid JSON). That can hide unexpected failures like body stream errors and make debugging harder. Consider catching the error, ignoring only SyntaxError (non-JSON bodies), and rethrowing anything else.

Suggested change
} catch {
// response body is not valid JSON
} catch (error) {
// response body is not valid JSON; only ignore JSON syntax errors
if (!(error instanceof SyntaxError)) {
throw error;
}

Copilot uses AI. Check for mistakes.
Comment on lines 20 to +23
"test": "vitest",
"lint": "biome check --write .",
"lint:ci": "biome ci",
"typecheck": "tsc --noEmit"
"typecheck": "pnpm exec tsc --noEmit"
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

This script change (wrapping tsc with pnpm exec) isn't mentioned in the PR description and is unrelated to the ENSRainbow JSON error handling change. Please either update the PR description to explain why this is needed, or move this to a separate PR to keep the change set focused.

Copilot uses AI. Check for mistakes.
} catch {
// response body is not valid JSON
}
throw new Error(message ?? `Failed to fetch ENSRainbow config: ${response.statusText}`);
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The PR checklist claims this "does not change runtime behavior or semantics", but this change intentionally alters error behavior for non-OK + non-JSON responses (previously a SyntaxError could propagate; now it becomes a generic Error with a fallback message). Please update the PR description/checklist to reflect that this is a behavioral change in error handling.

Copilot uses AI. Check for mistakes.
json: () => Promise.reject(new SyntaxError("Unexpected token")),
});

await expect(client.config()).rejects.toThrow("Failed to fetch ENSRainbow config: Not Found");
Copy link
Contributor

Choose a reason for hiding this comment

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

Line 353 in client.test.ts exceeds the 100 character line width limit configured in biome.jsonc

Fix on Vercel

Comment on lines +407 to +408
} catch {
// response body is not valid JSON
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
} catch {
// response body is not valid JSON
} catch (error) {
// Only ignore SyntaxError from non-JSON bodies
if (!(error instanceof SyntaxError)) {
throw error;
}

Bare catch block masks unexpected errors from response.json() beyond SyntaxError, hiding stream failures and other runtime issues

Fix on Vercel

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