getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.98k stars 1.57k forks source link

Sentry React Server Component error: message omitted despite instrumentation setup #14164

Open seeARMS opened 2 days ago

seeARMS commented 2 days ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

8.36

Framework Version

Next 15 canary

Link to Sentry event

https://paragraph.sentry.io/issues/5850530388/?project=6606494&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=issue-stream&statsPeriod=14d&stream_index=3

Reproduction Example/SDK Setup

// instrumentation.ts
import * as Sentry from "@sentry/nextjs"

export const onRequestError = Sentry.captureRequestError

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    await import("./sentry.server.config")
  }

  if (process.env.NEXT_RUNTIME === "edge") {
    await import("./sentry.edge.config")
  }
}
// sentry.server.config
import { nodeProfilingIntegration } from "@sentry/profiling-node"
import { initSentry } from "./sentry.config"

initSentry({
  integrations: [nodeProfilingIntegration()],
})

// sentry.config
import { addBreadcrumb, captureMessage, init } from "@sentry/nextjs"
import type { BrowserOptions, ErrorEvent, EventHint } from "@sentry/nextjs"
import {
  DENY_URLS,
  IGNORE_ERRORS,
  matchesFilenameIgnorelist,
  tracesSampler,
} from "./SENTRY_CONSTANTS"

export const SENTRY_DSN =
  process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN

export const isProd =
  process.env.NEXT_PUBLIC_VERCEL_ENV === "production" ||
  process.env.SENTRY_DEBUGGING === "true"

export function initSentry(options: Partial<BrowserOptions> = {}): void {
  if (!isProd) return

  init({
    release: process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA,
    denyUrls: DENY_URLS,
    dsn:
      SENTRY_DSN,
    tracesSampler: (samplingContext) => tracesSampler(samplingContext, 0.25),
    profilesSampleRate: 1.0,
    debug: process.env.SENTRY_DEBUGGING === "true",
    ignoreErrors: IGNORE_ERRORS,
    beforeSend: beforeSendHandler,
    enabled: isProd,
    ...options,
  })
}

function beforeSendHandler(
  event: ErrorEvent,
  hint: EventHint
): ErrorEvent | null {
  try {
    const originalException = hint.originalException?.toString()

    if (shouldIgnoreString(originalException)) {
      return null
    }

    const syntheticException = hint.syntheticException?.toString()

    if (shouldIgnoreString(syntheticException)) {
      return null
    }

    const frames = event.exception?.values?.[0]?.stacktrace?.frames || []
    if (
      frames.some(
        (frame) =>
          shouldIgnoreString(frame.filename) ||
          shouldIgnoreString(frame.module) ||
          shouldIgnoreString(frame.function)
      )
    ) {
      return null
    }

    if (shouldIgnoreString(event?.exception?.values?.[0]?.value)) {
      return null
    }
  } catch (e) {
    console.error("Error filtering Sentry event", e)
    addBreadcrumb({
      message: "Error filtering Sentry event",
      data: { error: e, event },
    })
    captureMessage("Error filtering Sentry event")
  }

  return event
}

function shouldIgnoreString(str: string | undefined): boolean {
  if (!str) return false

  // Check if the string matches any pattern in IGNORE_ERRORS
  const matchesIgnoreErrors = IGNORE_ERRORS.some((ignore) => {
    if (typeof ignore === "string") {
      return str.toLowerCase().includes(ignore.toLowerCase())
    } else if (ignore instanceof RegExp) {
      return ignore.test(str)
    }
    return false
  })

  // Check if the string matches the filename ignore list
  const matchesFilenameIgnore = matchesFilenameIgnorelist(str)

  return matchesIgnoreErrors || matchesFilenameIgnore
}

Steps to Reproduce

I'm running Next 15 canary and the latest Sentry NextJS SDK (8.36).

I have the onRequestError configured in instrumentation.ts as per these docs, and I'm also properly importing the corresponding server/edge configs via instrumentation as per these docs.

Despite doing that, I'm still getting the stacktrace & error messages omitted in React server component errors.

Expected Result

The full stacktrace should be included.

Actual Result

The stacktrace and error message is omitted: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details

lforst commented 2 days ago

Hi, this is a limitation imposed by Next.js.

In theory, you should see the corresponding server error in the trace attached to the error event. That does not seem to be the case. Unfortunately we cannot know why that is - potentially because the server side event got dropped due to some filtering logic on your end.