|
1 |
| -import { captureException, getClient, getTraceMetaTags } from '@sentry/core'; |
| 1 | +import { captureException, flush, getClient, getTraceMetaTags } from '@sentry/core'; |
2 | 2 | import type { ClientOptions, Context } from '@sentry/types';
|
3 |
| -import { dropUndefinedKeys } from '@sentry/utils'; |
| 3 | +import { dropUndefinedKeys, logger, vercelWaitUntil } from '@sentry/utils'; |
4 | 4 | import type { VueOptions } from '@sentry/vue/src/types';
|
5 | 5 | import type { CapturedErrorContext } from 'nitropack';
|
6 | 6 | import type { NuxtRenderHTMLContext } from 'nuxt/app';
|
@@ -80,3 +80,31 @@ export function reportNuxtError(options: {
|
80 | 80 | });
|
81 | 81 | });
|
82 | 82 | }
|
| 83 | + |
| 84 | +/** |
| 85 | + * Flushes pending Sentry events with a 2 seconds timeout and in a way that cannot create unhandled promise rejections. |
| 86 | + * |
| 87 | + */ |
| 88 | +export async function flushSafelyWithTimeout(isDebug: boolean): Promise<void> { |
| 89 | + try { |
| 90 | + isDebug && logger.log('Flushing events...'); |
| 91 | + await flush(2000); |
| 92 | + isDebug && logger.log('Done flushing events'); |
| 93 | + } catch (e) { |
| 94 | + isDebug && logger.log('Error while flushing events:\n', e); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Utility function for the Nuxt module runtime function as we always have to get the client instance to get |
| 100 | + * the `debug` option (we cannot access BUILD_DEBUG in the module runtime). |
| 101 | + * |
| 102 | + * This function should be called when Nitro ends a request (so Vercel can wait). |
| 103 | + */ |
| 104 | +export function vercelWaitUntilAndFlush(): void { |
| 105 | + const sentryClient = getClient(); |
| 106 | + |
| 107 | + if (sentryClient) { |
| 108 | + vercelWaitUntil(flushSafelyWithTimeout(sentryClient.getOptions().debug || false)); |
| 109 | + } |
| 110 | +} |
0 commit comments