Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
88 lines (75 loc) · 3.17 KB
/
Copy pathutils.ts
File metadata and controls
88 lines (75 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import type { ClientOptions, Context, SerializedTraceData } from '@sentry/core';
import { captureException, debug, getClient, getTraceMetaTags } from '@sentry/core';
import type { CapturedErrorContext } from 'nitropack/types';
import type { NuxtRenderHTMLContext } from 'nuxt/app';
import type { ComponentPublicInstance } from 'vue';
/**
* Extracts the relevant context information from the error context (H3Event in Nitro Error)
* and created a structured context object.
*/
export function extractErrorContext(errorContext: CapturedErrorContext | undefined): Context {
const ctx: Context = {};
if (!errorContext) {
return ctx;
}
if (errorContext.event) {
ctx.method = errorContext.event._method;
ctx.path = errorContext.event._path;
}
if (Array.isArray(errorContext.tags)) {
ctx.tags = errorContext.tags;
}
return ctx;
}
/**
* Adds Sentry tracing <meta> tags to the returned html page.
*
* Exported only for testing
*/
export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head'], traceData?: SerializedTraceData): void {
const metaTags = getTraceMetaTags(traceData);
if (head.some(tag => tag.includes('meta') && tag.includes('sentry-trace'))) {
debug.warn(
'Skipping addition of meta tags. Sentry tracing meta tags are already present in HTML page. Make sure to only set up Sentry once on the server-side. ',
);
return;
}
if (metaTags) {
debug.log('Adding Sentry tracing meta tags to HTML page:', metaTags);
head.push(metaTags);
}
}
/**
* Reports an error to Sentry. This function is similar to `attachErrorHandler` in `@sentry/vue`.
* The Nuxt SDK does not register an error handler, but uses the Nuxt error hooks to report errors.
*
* We don't want to use the error handling from `@sentry/vue` as it wraps the existing error handler, which leads to a 500 error: https://github.com/getsentry/sentry-javascript/issues/12515
*/
export function reportNuxtError(options: {
error: unknown;
instance?: ComponentPublicInstance | null;
info?: string;
}): void {
const { error, instance, info } = options;
const metadata: Record<string, unknown> = {
info,
// todo: add component name and trace (like in the vue integration)
};
if (instance?.$props) {
const sentryClient = getClient();
// `attachProps` is defined in the Vue integration options, but the type is not exported from @sentry/vue, as it's only used internally.
const sentryOptions = sentryClient ? (sentryClient.getOptions() as ClientOptions & { attachProps: boolean }) : null;
// `attachProps` is enabled by default and props should only not be attached if explicitly disabled (see DEFAULT_CONFIG in `vueIntegration`).
// oxlint-disable-next-line typescript/no-unsafe-member-access
if (sentryOptions?.attachProps && instance.$props !== false) {
metadata.propsData = instance.$props;
}
}
// Capture exception in the next event loop, to make sure that all breadcrumbs are recorded in time.
setTimeout(() => {
captureException(error, {
captureContext: { contexts: { nuxt: metadata } },
mechanism: { handled: false, type: `auto.function.nuxt.${instance ? 'vue' : 'app'}-error` },
});
});
}