Skip to content
Closed
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
3 changes: 1 addition & 2 deletions packages/hono/src/shared/middlewareHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
winterCGRequestToRequestData,
} from '@sentry/core';
import type { Context } from 'hono';
import { hasFetchEvent } from '../utils/hono-context';
import { defaultShouldHandleError } from './defaultShouldHandleError';
import { resolveRouteName } from './resolveRouteName';
import { type SentryHonoMiddlewareOptions } from '../shared/types';
Expand All @@ -29,7 +28,7 @@ export function requestHandler(context: Context, getConnInfo?: GetConnInfo): voi
updateSpanRouteName(isolationScope, context);

isolationScope.setSDKProcessingMetadata({
normalizedRequest: winterCGRequestToRequestData(hasFetchEvent(context) ? context.event.request : context.req.raw),
normalizedRequest: winterCGRequestToRequestData(context.req.raw),
});

if (getConnInfo) {
Expand Down
15 changes: 0 additions & 15 deletions packages/hono/src/utils/hono-context.ts

This file was deleted.

45 changes: 39 additions & 6 deletions packages/hono/test/shared/middlewareHandlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as SentryCore from '@sentry/core';
import { SENTRY_SEGMENT_NAME_SOURCE, HTTP_ROUTE } from '@sentry/conventions/attributes';
import { Context } from 'hono';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { requestHandler, responseHandler } from '../../src/shared/middlewareHandlers';

Expand All @@ -8,10 +9,6 @@ vi.mock('hono/route', () => ({
matchedRoutes: () => [{ basePath: '/', path: '/test', method: 'GET', handler: (_c: unknown) => undefined }],
}));

vi.mock('../../src/utils/hono-context', () => ({
hasFetchEvent: () => false,
}));

const mockSetTransactionName = vi.fn();
const mockSetSDKProcessingMetadata = vi.fn();
const mockSetUser = vi.fn();
Expand Down Expand Up @@ -44,12 +41,14 @@ vi.mock('@sentry/core', async () => {
})),
getClient: vi.fn(() => undefined),
captureException: vi.fn(),
winterCGRequestToRequestData: vi.fn(actual.winterCGRequestToRequestData),
};
});

const getClientMock = SentryCore.getClient as ReturnType<typeof vi.fn>;
const captureExceptionMock = SentryCore.captureException as ReturnType<typeof vi.fn>;
const getActiveSpanMock = SentryCore.getActiveSpan as ReturnType<typeof vi.fn>;
const winterCGRequestToRequestDataMock = SentryCore.winterCGRequestToRequestData as ReturnType<typeof vi.fn>;

function createMockContext(status: number, error?: Error): unknown {
return {
Expand Down Expand Up @@ -185,11 +184,21 @@ describe('responseHandler', () => {
});

describe('transaction name', () => {
it('sets transaction name on isolation scope', () => {
it('sets request metadata and transaction name without reading the event', () => {
const context = createMockContext(200) as ReturnType<typeof createMockContext> & {
req: { raw: Request };
};
const eventGetter = vi.fn(() => {
throw new Error('This context has no FetchEvent');
});
Object.defineProperty(context, 'event', { get: eventGetter });

// oxlint-disable-next-line typescript/no-explicit-any
requestHandler(createMockContext(200) as any);
requestHandler(context as any);

expect(mockSetTransactionName).toHaveBeenCalledWith('GET /test');
expect(winterCGRequestToRequestDataMock).toHaveBeenCalledWith(context.req.raw);
expect(eventGetter).not.toHaveBeenCalled();
});

it('sets http.route and segment name source on the root span', () => {
Expand Down Expand Up @@ -332,3 +341,27 @@ describe('requestHandler — connection info', () => {
expect(mockSetUser).not.toHaveBeenCalled();
});
});

describe('requestHandler — request metadata', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('uses the same request exposed by a legacy FetchEvent context', () => {
const request = new Request('http://localhost/test?source=event');
const context = new Context(request, {
env: {},
executionCtx: {
request,
respondWith: vi.fn(),
passThroughOnException: vi.fn(),
waitUntil: vi.fn(),
},
});

requestHandler(context);

expect(context.event.request).toBe(context.req.raw);
expect(winterCGRequestToRequestDataMock).toHaveBeenCalledWith(request);
});
});
Loading