Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7b0353

Browse files
committed
test: added middleware span error test
1 parent 2703fd4 commit e7b0353

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { defineEventHandler, setHeader } from '#imports';
1+
import { defineEventHandler, setHeader,getQuery } from '#imports';
22

33
export default defineEventHandler(async event => {
4+
// Check if we should throw an error
5+
const query = getQuery(event);
6+
if (query.throwError === 'true') {
7+
throw new Error('Auth middleware error');
8+
}
9+
410
// Set a header to indicate this middleware ran
511
setHeader(event, 'x-auth-middleware', 'executed');
612
});

‎dev-packages/e2e-tests/test-applications/nuxt-3/tests/middleware.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from '@playwright/test';
2-
import { waitForTransaction } from '@sentry-internal/test-utils';
2+
import { waitForTransaction,waitForError } from '@sentry-internal/test-utils';
33

44
test.describe('Server Middleware Instrumentation', () => {
55
test('should create separate spans for each server middleware', async ({ request }) => {
@@ -75,4 +75,47 @@ test.describe('Server Middleware Instrumentation', () => {
7575
expect(span.parent_span_id).toBe(serverTxnEvent.contexts?.trace?.span_id);
7676
});
7777
});
78+
79+
test('should capture errors thrown in middleware and associate them with the span', async ({ request }) => {
80+
const serverTxnEventPromise = waitForTransaction('nuxt-3', txnEvent => {
81+
return txnEvent.transaction?.includes('GET /api/middleware-test') ?? false;
82+
});
83+
84+
const errorEventPromise = waitForError('nuxt-3', errorEvent => {
85+
return errorEvent?.exception?.values?.[0]?.value === 'Auth middleware error';
86+
});
87+
88+
// Make request with query param to trigger error in auth middleware
89+
const response = await request.get('/api/middleware-test?throwError=true');
90+
91+
// The request should fail due to the middleware error
92+
expect(response.status()).toBe(500);
93+
94+
const [serverTxnEvent, errorEvent] = await Promise.all([serverTxnEventPromise, errorEventPromise]);
95+
96+
// Find the auth middleware span
97+
const authMiddlewareSpan = serverTxnEvent.spans?.find(
98+
span => span.op === 'http.server.middleware' && span.data?.['nuxt.middleware.name'] === '03.auth.ts',
99+
);
100+
101+
expect(authMiddlewareSpan).toBeDefined();
102+
103+
// Verify the span has error status
104+
expect(authMiddlewareSpan?.status).toBe('internal_error');
105+
106+
// Verify the error event is associated with the correct transaction
107+
expect(errorEvent.transaction).toContain('GET /api/middleware-test');
108+
109+
// Verify the error has the correct mechanism
110+
expect(errorEvent.exception?.values?.[0]).toEqual(
111+
expect.objectContaining({
112+
value: 'Auth middleware error',
113+
type: 'Error',
114+
mechanism: expect.objectContaining({
115+
handled: false,
116+
type: 'auto.http.nuxt',
117+
}),
118+
}),
119+
);
120+
});
78121
});
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { defineEventHandler, setHeader } from '#imports';
1+
import { defineEventHandler, setHeader,getQuery } from '#imports';
22

33
export default defineEventHandler(async event => {
4+
// Check if we should throw an error
5+
const query = getQuery(event);
6+
if (query.throwError === 'true') {
7+
throw new Error('Auth middleware error');
8+
}
9+
410
// Set a header to indicate this middleware ran
511
setHeader(event, 'x-auth-middleware', 'executed');
612
});

‎dev-packages/e2e-tests/test-applications/nuxt-4/tests/middleware.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from '@playwright/test';
2-
import { waitForTransaction } from '@sentry-internal/test-utils';
2+
import { waitForTransaction,waitForError } from '@sentry-internal/test-utils';
33

44
test.describe('Server Middleware Instrumentation', () => {
55
test('should create separate spans for each server middleware', async ({ request }) => {
@@ -75,4 +75,47 @@ test.describe('Server Middleware Instrumentation', () => {
7575
expect(span.parent_span_id).toBe(serverTxnEvent.contexts?.trace?.span_id);
7676
});
7777
});
78+
79+
test('should capture errors thrown in middleware and associate them with the span', async ({ request }) => {
80+
const serverTxnEventPromise = waitForTransaction('nuxt-4', txnEvent => {
81+
return txnEvent.transaction?.includes('GET /api/middleware-test') ?? false;
82+
});
83+
84+
const errorEventPromise = waitForError('nuxt-4', errorEvent => {
85+
return errorEvent?.exception?.values?.[0]?.value === 'Auth middleware error';
86+
});
87+
88+
// Make request with query param to trigger error in auth middleware
89+
const response = await request.get('/api/middleware-test?throwError=true');
90+
91+
// The request should fail due to the middleware error
92+
expect(response.status()).toBe(500);
93+
94+
const [serverTxnEvent, errorEvent] = await Promise.all([serverTxnEventPromise, errorEventPromise]);
95+
96+
// Find the auth middleware span
97+
const authMiddlewareSpan = serverTxnEvent.spans?.find(
98+
span => span.op === 'http.server.middleware' && span.data?.['nuxt.middleware.name'] === '03.auth.ts',
99+
);
100+
101+
expect(authMiddlewareSpan).toBeDefined();
102+
103+
// Verify the span has error status
104+
expect(authMiddlewareSpan?.status).toBe('internal_error');
105+
106+
// Verify the error event is associated with the correct transaction
107+
expect(errorEvent.transaction).toContain('GET /api/middleware-test');
108+
109+
// Verify the error has the correct mechanism
110+
expect(errorEvent.exception?.values?.[0]).toEqual(
111+
expect.objectContaining({
112+
value: 'Auth middleware error',
113+
type: 'Error',
114+
mechanism: expect.objectContaining({
115+
handled: false,
116+
type: 'auto.http.nuxt',
117+
}),
118+
}),
119+
);
120+
});
78121
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /