diff --git a/packages/mcp-core/src/api-client/client.test.ts b/packages/mcp-core/src/api-client/client.test.ts index 88ef96637..131e5c108 100644 --- a/packages/mcp-core/src/api-client/client.test.ts +++ b/packages/mcp-core/src/api-client/client.test.ts @@ -9,6 +9,7 @@ import { ConfigurationError } from "../errors"; import { parseSentryUrl } from "../internal/url-helpers"; import { SentryApiService } from "./client"; import { + ApiClientError, ApiNotFoundError, ApiPermissionError, ApiServerError, @@ -106,6 +107,71 @@ describe("validateEvents error bodies", () => { ); }); +describe("validateEvents non-JSON responses", () => { + afterEach(() => mswServer.resetHandlers()); + + it("classifies the endpoint's empty feature-gate 400 as a client error", async () => { + mswServer.use( + http.get( + "https://sentry.io/api/0/organizations/test-org/events/validate/", + () => new HttpResponse(null, { status: 400 }), + ), + ); + const api = new SentryApiService({ + host: "sentry.io", + accessToken: "test-token", + }); + await expect( + api.validateEvents({ organizationSlug: "test-org" }), + ).rejects.toBeInstanceOf(ApiValidationError); + }); + + it.each(["text/plain", "text/html"])( + "classifies a 400 %s response as a client error", + async (contentType) => { + mswServer.use( + http.get( + "https://sentry.io/api/0/organizations/test-org/events/validate/", + () => + new HttpResponse("Invalid query", { + status: 400, + headers: { "Content-Type": contentType }, + }), + ), + ); + const api = new SentryApiService({ + host: "sentry.io", + accessToken: "test-token", + }); + await expect( + api.validateEvents({ organizationSlug: "test-org" }), + ).rejects.toBeInstanceOf(ApiValidationError); + }, + ); + + it("does not classify a successful HTML response as user input", async () => { + mswServer.use( + http.get( + "https://sentry.io/api/0/organizations/test-org/events/validate/", + () => + new HttpResponse("Unexpected response", { + status: 200, + headers: { "Content-Type": "text/html" }, + }), + ), + ); + const api = new SentryApiService({ + host: "sentry.io", + accessToken: "test-token", + }); + const result = api.validateEvents({ organizationSlug: "test-org" }); + await expect(result).rejects.toThrow( + "Expected JSON response but received HTML", + ); + await expect(result).rejects.not.toBeInstanceOf(ApiClientError); + }); +}); + describe("API bearer token validation", () => { it("removes edge padding before sending a request", async () => { let authorization: string | null = null; diff --git a/packages/mcp-core/src/api-client/client.ts b/packages/mcp-core/src/api-client/client.ts index a4946e1ae..7bb1b1708 100644 --- a/packages/mcp-core/src/api-client/client.ts +++ b/packages/mcp-core/src/api-client/client.ts @@ -1001,15 +1001,21 @@ export class SentryApiService { responseText.includes("