Skip to content

Fix crash on non-JSON error response body - #2234

Merged
Strift merged 2 commits into
meilisearch:mainfrom
soroush5:fix/non-json-response-body
Sep 16, 2026
Merged

Strift merged 2 commits into
meilisearch:mainfrom
soroush5:fix/non-json-response-body

Conversation

@soroush5

@soroush5 soroush5 commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

When the server (or a proxy in front of it) returns a non-JSON error body — e.g. an nginx 502 Bad Gateway HTML page — the client threw a raw SyntaxError: Unexpected token '<'... instead of a MeilisearchApiError, so callers couldn't handle it as a normal API error.

This parses the body defensively (falling back to undefined, like the empty-body case already did) so error responses always surface as MeilisearchApiError with the HTTP status. Same symptom as #1892.

Testing

  • 2 new tests (HTML body, plain-text body): vitest run tests/http-requests.test.ts — 8 passed.
  • tsc --noEmit, oxlint, and prettier --check clean.

Summary by CodeRabbit

  • Bug Fixes

    • Error responses containing HTML, plain text, or other invalid JSON now consistently return an API error.
    • Streaming requests now handle invalid error-response bodies consistently.
    • Successful responses with invalid JSON now surface the JSON parsing error directly.
    • Error details are omitted when an error response cannot be parsed as JSON.
  • Tests

    • Added coverage for standard and streaming requests receiving non-JSON error responses.

@coderabbitai

coderabbitai Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 055cb5c4-d797-4d08-9717-4ecda07ef93e

📥 Commits

Reviewing files that changed from the base of the PR and between 3393171 and d008603.

📒 Files selected for processing (2)
  • src/http-requests.ts
  • tests/http-requests.test.ts

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The HTTP request paths now propagate invalid JSON errors for successful responses and convert invalid JSON in error responses into MeilisearchApiError. Tests cover HTML and plain-text error bodies for regular and streaming requests.

Changes

Response Parsing

Layer / File(s) Summary
Response parsing and error handling
src/http-requests.ts, tests/http-requests.test.ts
parseResponseBody now propagates invalid JSON errors. Regular requests rethrow parse errors for successful responses and create MeilisearchApiError without parsed details for failed responses. Streaming requests apply the same error handling. Tests cover HTML and plain-text error bodies.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to d0086

The response-handling change has no identified actionable merge risk.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing crashes when error responses contain non-JSON bodies.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@codecov

codecov Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.15%. Comparing base (b9d8cdf) to head (d008603).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2234      +/-   ##
==========================================
+ Coverage   98.14%   98.15%   +0.01%     
==========================================
  Files          14       14              
  Lines         700      706       +6     
  Branches      110      106       -4     
==========================================
+ Hits          687      693       +6     
  Misses         12       12              
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Bare JSON.parse threw a raw SyntaxError instead of MeilisearchApiError
when the server (or a proxy) returned a non-JSON body. Parse via a
helper that falls back to undefined, so error responses surface as
MeilisearchApiError with 'status: statusText'.
@soroush5
soroush5 force-pushed the fix/non-json-response-body branch from 7acacc8 to 3393171 Compare September 9, 2026 16:10
@Strift Strift added the enhancement New feature or request label Sep 16, 2026
@Strift
Strift enabled auto-merge September 16, 2026 05:54

@Strift Strift left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hello @soroush5 and thx for the fix 🙏

@Strift
Strift added this pull request to the merge queue Sep 16, 2026
@Strift Strift added bug Something isn't working and removed enhancement New feature or request labels Sep 16, 2026
Merged via the queue into meilisearch:main with commit c78ae4c Sep 16, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants