Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion packages/mcp-core/src/api-client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,78 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ConfigurationError } from "../errors";
import { parseSentryUrl } from "../internal/url-helpers";
import { SentryApiService } from "./client";
import { ApiNotFoundError, ApiPermissionError, ApiServerError } from "./errors";
import {
ApiClientError,
ApiNotFoundError,
ApiPermissionError,
ApiServerError,
ApiValidationError,
} from "./errors";

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("<html>Unexpected response</html>", {
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 () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/mcp-core/src/api-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,15 +973,21 @@ export class SentryApiService {
responseText.includes("<html")
) {
// HTML when expecting JSON usually indicates authentication or routing issues
throw new Error(
throw createApiError(
`Expected JSON response but received HTML (${response.status} ${response.statusText}). This may indicate you're not authenticated, the URL is incorrect, or there's a server issue.`,
response.status,
undefined,
responseText,
);
}

// Generic non-JSON error
throw new Error(
throw createApiError(
`Expected JSON response but received ${contentType || "unknown content type"} ` +
`(${response.status} ${response.statusText})`,
response.status,
undefined,
responseText,
);
}

Expand Down
Loading