import * as Sentry from "@sentry/nextjs" import { isCustomCause } from "@scandic-hotels/trpc/errors" import { env } from "./env/server" import type { TRPCError } from "@trpc/server" export const denyUrls: (string | RegExp)[] = [ // Ignore preview urls /\/.{2}\/preview\//, ] export async function register() { await configureSentry() } export const onRequestError = Sentry.captureRequestError async function configureSentry() { Sentry.init({ dsn: "https://fe39c070b4154e2f9cc35f0e5de0aedb@o4508102497206272.ingest.de.sentry.io/4508102500286544", environment: env.SENTRY_ENVIRONMENT, enabled: env.SENTRY_ENVIRONMENT !== "development", tracesSampleRate: env.SENTRY_SERVER_SAMPLERATE, denyUrls: denyUrls, enableLogs: true, enableMetrics: true, release: env.RELEASE_TAG || undefined, beforeSend(event, hint) { const error = hint.originalException // Don't send TRPCErrors with client error codes if (isTRPCError(error)) { const clientErrorCodes = ["CONFLICT", "NOT_FOUND", "UNAUTHORIZED"] if (clientErrorCodes.includes(error.code)) { return null // Don't send to Sentry } if (isCustomCause(error.cause)) { event.contexts = event.contexts || {} event.contexts.errorDetails = { data: error.cause.errorDetails } } } return event }, }) } function isTRPCError(error: unknown): error is TRPCError { return error instanceof Error && error.name === "TRPCError" }