fix(logs): replace .parse() with .safeParse() to prevent ZodError crash on self-hosted#1096
Open
jared-outpost[bot] wants to merge 1 commit into
Open
fix(logs): replace .parse() with .safeParse() to prevent ZodError crash on self-hosted#1096jared-outpost[bot] wants to merge 1 commit into
jared-outpost[bot] wants to merge 1 commit into
Conversation
…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
Contributor
|
Contributor
Codecov Results 📊✅ Patch coverage is 100.00%. Project has 5012 uncovered lines. 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 -1Generated by Codecov Action |
BYK
approved these changes
Jun 12, 2026
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." | ||
| ); |
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
listLogs()andgetLogsBatch()insrc/lib/api/logs.tswere the only API functions using raw Zod.parse(), which throws an unhandledZodErrorwhen 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 byapiRequestToRegionandorganizations.ts.Added a
safeParseResponse()helper that pre-checkstypeof 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 bothlistLogsandgetLogs. Full API test suite (182 tests) passes.Closes #1095