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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureException, httpRequestToRequestData, withScope } from '@sentry/core';
import { captureException, getIsolationScope, httpRequestToRequestData, withScope } from '@sentry/core';
import type { NextPageContext } from 'next';
import { flushSafelyWithTimeout, waitUntil } from '../utils/responseEnd';

Expand Down Expand Up @@ -57,6 +57,9 @@ export async function captureUnderscoreErrorException(contextOrProps: ContextOrP
});
});

// Set the lastEventId on the isolation scope so it's accessible via lastEventId()
getIsolationScope().setLastEventId(eventId);

waitUntil(flushSafelyWithTimeout());

return eventId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { lastEventId } from '@sentry/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { captureUnderscoreErrorException } from '../../../src/common/pages-router-instrumentation/_error';

let storedLastEventId: string | undefined = undefined;

const mockCaptureException = vi.fn(() => 'test-event-id');
const mockWithScope = vi.fn((callback: (scope: any) => any) => {
const mockScope = {
setSDKProcessingMetadata: vi.fn(),
};
return callback(mockScope);
});
const mockLastEventId = vi.fn(() => storedLastEventId);
const mockGetIsolationScope = vi.fn(() => ({
setLastEventId: (id: string | undefined) => {
storedLastEventId = id;
},
lastEventId: () => storedLastEventId,
}));

vi.mock('@sentry/core', async () => {
const actual = await vi.importActual('@sentry/core');
Expand All @@ -16,6 +26,8 @@ vi.mock('@sentry/core', async () => {
captureException: (...args: unknown[]) => mockCaptureException(...args),
withScope: (callback: (scope: any) => any) => mockWithScope(callback),
httpRequestToRequestData: vi.fn(() => ({ url: 'http://test.com' })),
lastEventId: () => mockLastEventId(),
getIsolationScope: () => mockGetIsolationScope(),
};
});

Expand All @@ -27,6 +39,7 @@ vi.mock('../../../src/common/utils/responseEnd', () => ({
describe('captureUnderscoreErrorException', () => {
beforeEach(() => {
vi.clearAllMocks();
storedLastEventId = undefined;
});

afterEach(() => {
Expand Down Expand Up @@ -114,4 +127,19 @@ describe('captureUnderscoreErrorException', () => {
expect(result).toBeUndefined();
expect(mockCaptureException).not.toHaveBeenCalled();
});

it('lastEventId() should return the event ID after captureUnderscoreErrorException', async () => {
const error = new Error('Test error');
const eventId = await captureUnderscoreErrorException({
err: error,
pathname: '/test',
res: { statusCode: 500 } as any,
});

expect(eventId).toBe('test-event-id');
expect(mockCaptureException).toHaveBeenCalled();

const lastId = lastEventId();
expect(lastId).toBe('test-event-id');
});
});
Loading