-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(logs): replace .parse() with .safeParse() to prevent ZodError crash on self-hosted #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jared-outpost
wants to merge
1
commit into
main
Choose a base branch
from
issue-1095-fix-log-list-zod-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /** | ||
| * Tests for listLogs and getLogs — guards against non-object SDK responses. | ||
| * | ||
| * CLI-20C: self-hosted instances can return non-object data (plain text, HTML) | ||
| * from the /events/?dataset=logs endpoint when the logs dataset is unsupported | ||
| * or a reverse proxy intercepts the request. Previously this crashed with an | ||
| * unhandled ZodError; now it throws a descriptive ApiError. | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, test } from "vitest"; | ||
| import { getLogs, listLogs } from "../../../src/lib/api/logs.js"; | ||
| import { setAuthToken } from "../../../src/lib/db/auth.js"; | ||
| import { ApiError } from "../../../src/lib/errors.js"; | ||
| import { mockFetch, useTestConfigDir } from "../../helpers.js"; | ||
|
|
||
| useTestConfigDir("logs-api-test-"); | ||
|
|
||
| let originalFetch: typeof globalThis.fetch; | ||
|
|
||
| beforeEach(async () => { | ||
| originalFetch = globalThis.fetch; | ||
| await setAuthToken("fake-token-for-test", 3600); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| /** | ||
| * Mock fetch to return a fixed JSON body for all requests. | ||
| * The SDK parses the response via response.json(), so wrapping in | ||
| * JSON.stringify ensures the SDK receives the raw value as `data`. | ||
| */ | ||
| function mockOk(body: unknown) { | ||
| globalThis.fetch = mockFetch( | ||
| async () => | ||
| new Response(JSON.stringify(body), { | ||
| status: 200, | ||
| headers: { "Content-Type": "application/json" }, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| describe("listLogs", () => { | ||
| test("returns logs when API returns a valid response", async () => { | ||
| mockOk({ | ||
| data: [ | ||
| { | ||
| "sentry.item_id": "log-001", | ||
| timestamp: "2025-01-30T14:32:15+00:00", | ||
| timestamp_precise: 1_770_060_419_044_800_300, | ||
| message: "Test log message", | ||
| severity: "info", | ||
| trace: "abc123def456abc123def456abc12345", | ||
| }, | ||
| ], | ||
| meta: { fields: {} }, | ||
| }); | ||
|
|
||
| const logs = await listLogs("test-org", "test-project"); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0]["sentry.item_id"]).toBe("log-001"); | ||
| }); | ||
|
|
||
| test("throws ApiError when API returns a string instead of object", async () => { | ||
| mockOk("Proxy error: upstream not found"); | ||
|
|
||
| await expect(listLogs("test-org", "test-project")).rejects.toThrow( | ||
| ApiError | ||
| ); | ||
|
|
||
| try { | ||
| await listLogs("test-org", "test-project"); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(ApiError); | ||
| const apiError = error as ApiError; | ||
| expect(apiError.message).toContain("unexpected response format"); | ||
| expect(apiError.detail).toContain("received string"); | ||
| } | ||
| }); | ||
|
|
||
| test("throws ApiError when API returns null", async () => { | ||
| mockOk(null); | ||
|
|
||
| await expect(listLogs("test-org", "test-project")).rejects.toThrow( | ||
| ApiError | ||
| ); | ||
| }); | ||
|
|
||
| test("throws ApiError when response has wrong shape", async () => { | ||
| mockOk({ wrong: "shape" }); | ||
|
|
||
| await expect(listLogs("test-org", "test-project")).rejects.toThrow( | ||
| ApiError | ||
| ); | ||
|
|
||
| try { | ||
| await listLogs("test-org", "test-project"); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(ApiError); | ||
| expect((error as ApiError).message).toContain( | ||
| "unexpected response format" | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("getLogs", () => { | ||
| test("returns logs when API returns a valid detailed response", async () => { | ||
| mockOk({ | ||
| data: [ | ||
| { | ||
| "sentry.item_id": "log-001", | ||
| timestamp: "2025-01-30T14:32:15+00:00", | ||
| timestamp_precise: 1_770_060_419_044_800_300, | ||
| message: "Test log message", | ||
| severity: "info", | ||
| trace: "abc123def456abc123def456abc12345", | ||
| project: "test-project", | ||
| environment: "production", | ||
| release: "1.0.0", | ||
| "sdk.name": "sentry.javascript.node", | ||
| "sdk.version": "8.0.0", | ||
| span_id: "abc123def456abc1", | ||
| "code.function": "main", | ||
| "code.file.path": "/app/index.ts", | ||
| "code.line.number": "42", | ||
| "sentry.otel.kind": "INTERNAL", | ||
| "sentry.otel.status_code": "OK", | ||
| "sentry.otel.instrumentation_scope.name": "my-app", | ||
| }, | ||
| ], | ||
| meta: { fields: {} }, | ||
| }); | ||
|
|
||
| const logs = await getLogs("test-org", "test-project", ["log-001"]); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0]["sentry.item_id"]).toBe("log-001"); | ||
| }); | ||
|
|
||
| test("throws ApiError when API returns a string instead of object", async () => { | ||
| mockOk("<html><body>502 Bad Gateway</body></html>"); | ||
|
|
||
| await expect( | ||
| getLogs("test-org", "test-project", ["log-001"]) | ||
| ).rejects.toThrow(ApiError); | ||
|
|
||
| try { | ||
| await getLogs("test-org", "test-project", ["log-001"]); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(ApiError); | ||
| const apiError = error as ApiError; | ||
| expect(apiError.message).toContain("unexpected response format"); | ||
| expect(apiError.detail).toContain("received string"); | ||
| expect(apiError.detail).toContain("self-hosted"); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
assertObjectResponsefunction logs a confusing error message, "received object", when the API response isnulldue to thetypeof null === "object"JavaScript quirk.Severity: LOW
Suggested Fix
Modify the error message generation in
assertObjectResponseto handle thenullcase specifically. Instead of just usingtypeof data, check ifdata === nulland 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
Did we get this right? 👍 / 👎 to inform future reviews.