|
1 |
| -import { patchEventHandler } from '@sentry-internal/nitro-utils'; |
2 |
| -import { GLOBAL_OBJ, flush, getClient, logger, vercelWaitUntil } from '@sentry/core'; |
| 1 | +import { |
| 2 | + GLOBAL_OBJ, |
| 3 | + flush, |
| 4 | + getClient, |
| 5 | + getDefaultIsolationScope, |
| 6 | + getIsolationScope, |
| 7 | + logger, |
| 8 | + vercelWaitUntil, |
| 9 | + withIsolationScope, |
| 10 | +} from '@sentry/core'; |
3 | 11 | import * as Sentry from '@sentry/node';
|
4 |
| -import { H3Error } from 'h3'; |
| 12 | +import { type EventHandler, H3Error } from 'h3'; |
5 | 13 | import { defineNitroPlugin } from 'nitropack/runtime';
|
6 | 14 | import type { NuxtRenderHTMLContext } from 'nuxt/app';
|
7 | 15 | import { addSentryTracingMetaTags, extractErrorContext } from '../utils';
|
@@ -66,3 +74,27 @@ async function flushWithTimeout(): Promise<void> {
|
66 | 74 | isDebug && logger.log('Error while flushing events:\n', e);
|
67 | 75 | }
|
68 | 76 | }
|
| 77 | + |
| 78 | +// copied from '@sentry-internal/nitro-utils' - the nuxt-module-builder does not inline devDependencies |
| 79 | +function patchEventHandler(handler: EventHandler): EventHandler { |
| 80 | + return new Proxy(handler, { |
| 81 | + async apply(handlerTarget, handlerThisArg, handlerArgs: Parameters<EventHandler>) { |
| 82 | + const isolationScope = getIsolationScope(); |
| 83 | + const newIsolationScope = isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope; |
| 84 | + |
| 85 | + logger.log( |
| 86 | + `Patched h3 event handler. ${ |
| 87 | + isolationScope === newIsolationScope ? 'Using existing' : 'Created new' |
| 88 | + } isolation scope.`, |
| 89 | + ); |
| 90 | + |
| 91 | + return withIsolationScope(newIsolationScope, async () => { |
| 92 | + try { |
| 93 | + return await handlerTarget.apply(handlerThisArg, handlerArgs); |
| 94 | + } finally { |
| 95 | + await flushIfServerless(); |
| 96 | + } |
| 97 | + }); |
| 98 | + }, |
| 99 | + }); |
| 100 | +} |
0 commit comments