Skip to content

fix(logs): replace .parse() with .safeParse() to prevent ZodError crash on self-hosted#1096

Open
jared-outpost[bot] wants to merge 1 commit into
mainfrom
issue-1095-fix-log-list-zod-crash
Open

fix(logs): replace .parse() with .safeParse() to prevent ZodError crash on self-hosted#1096
jared-outpost[bot] wants to merge 1 commit into
mainfrom
issue-1095-fix-log-list-zod-crash

Conversation

@jared-outpost

@jared-outpost jared-outpost Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

listLogs() and getLogsBatch() in src/lib/api/logs.ts were the only API functions using raw Zod .parse(), which throws an unhandled ZodError when a self-hosted instance returns a non-object response (HTML from a reverse proxy, plain text from an unsupported endpoint). Replaced with .safeParse() + ApiError, matching the pattern used by apiRequestToRegion and organizations.ts.

Added a safeParseResponse() helper that pre-checks typeof data, uses .safeParse(), and attaches Zod issues to Sentry context for diagnostics.

Testing

npx vitest run test/lib/api/logs.test.ts — 6 tests covering string, null, wrong-shape, and valid responses for both listLogs and getLogs. Full API test suite (182 tests) passes.

Closes #1095

…sh on self-hosted

listLogs() and getLogsBatch() used raw .parse() which throws ZodError
when the API returns a non-object response (e.g. HTML from a reverse
proxy or plain text from an unsupported self-hosted endpoint). This was
the only API module using .parse() — all others use .safeParse() via
apiRequestToRegion's schema option.

Added safeParseResponse() helper that:
1. Pre-checks typeof data for early, descriptive errors
2. Uses .safeParse() + wraps failures in ApiError
3. Attaches Zod issues to Sentry context for diagnostics

Fixes #1095
@github-actions

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-1096/

Built to branch gh-pages at 2026-06-12 08:04 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

✅ Patch coverage is 100.00%. Project has 5012 uncovered lines.
✅ Project coverage is 81.2%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    81.19%    81.20%    +0.01%
==========================================
  Files          383       383         —
  Lines        26649     26657        +8
  Branches     17340     17346        +6
==========================================
+ Hits         21636     21645        +9
- Misses        5013      5012        -1
- Partials      1798      1797        -1

Generated by Codecov Action

@MathurAditya724 MathurAditya724 requested a review from BYK June 12, 2026 12:06
@MathurAditya724 MathurAditya724 marked this pull request as ready for review June 13, 2026 05:24
Comment thread src/lib/api/logs.ts
Comment on lines +62 to +64
`Expected an object but received ${typeof data}. ` +
"This may indicate an incompatible self-hosted Sentry version or a proxy interfering with the response."
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The assertObjectResponse function logs a confusing error message, "received object", when the API response is null due to the typeof null === "object" JavaScript quirk.
Severity: LOW

Suggested Fix

Modify the error message generation in assertObjectResponse to handle the null case specifically. Instead of just using typeof data, check if data === null and print "null" in the message. For example: ...received ${data === null ? 'null' : typeof data}. This will provide a clear and accurate error message.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/lib/api/logs.ts#L62-L64

Potential issue: When the API response data is `null`, the `assertObjectResponse`
function correctly throws an `ApiError`. However, due to the JavaScript quirk where
`typeof null` evaluates to `"object"`, the error message becomes `"Expected an object
but received object"`. This is self-contradictory and misleading for users, especially
in self-hosted environments where a `null` response is a possible edge case. This makes
diagnosing the underlying issue more difficult.

Did we get this right? 👍 / 👎 to inform future reviews.

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.

Fix: log list crashes with ZodError on self-hosted instances (CLI-20C)

1 participant