diff --git a/src/app/api/sentry-example-error/route.js b/src/app/api/sentry-example-error/route.js deleted file mode 100644 index f486f3d..0000000 --- a/src/app/api/sentry-example-error/route.js +++ /dev/null @@ -1,9 +0,0 @@ -import { NextResponse } from "next/server"; - -export const dynamic = "force-dynamic"; - -// A faulty API route to test Sentry's error monitoring -export function GET() { - throw new Error("Sentry Example API Route Error"); - return NextResponse.json({ data: "Testing Sentry Error..." }); -} diff --git a/src/app/api/sentry-example-error/route.ts b/src/app/api/sentry-example-error/route.ts new file mode 100644 index 0000000..27b7cbe --- /dev/null +++ b/src/app/api/sentry-example-error/route.ts @@ -0,0 +1,20 @@ +import { NextResponse } from "next/server"; +import * as Sentry from "@sentry/nextjs"; + +import { getSession } from "next-auth/react"; + +export const dynamic = "force-dynamic"; + +export async function GET() { + const session = await getSession(); + + if (session?.user) { + Sentry.setUser({ + email: session.user.email ?? "", + id: session.user.id, + }); + } + + throw new Error("Sentry Example API Route Error"); + return NextResponse.json({ data: "Testing Sentry Error..." }); +} diff --git a/src/components/providers/index.tsx b/src/components/providers/index.tsx index 04c8feb..d2f8b02 100644 --- a/src/components/providers/index.tsx +++ b/src/components/providers/index.tsx @@ -1,6 +1,6 @@ "use client"; -import React from "react"; +import React, { useEffect } from "react"; import posthog from "posthog-js"; import { PostHogProvider, usePostHog } from "posthog-js/react"; import { SessionProvider, useSession } from "next-auth/react"; @@ -9,6 +9,7 @@ import { TRPCReactProvider } from "~/trpc/react"; import { env } from "~/env.mjs"; import { useSearchParams } from "next/navigation"; import { ThemeProvider } from "./theme-provider"; +import * as Sentry from "@sentry/nextjs"; if (typeof window !== "undefined" && process.env.NODE_ENV === "production") { posthog.init(env.NEXT_PUBLIC_POSTHOG_API_KEY!, { @@ -16,16 +17,20 @@ if (typeof window !== "undefined" && process.env.NODE_ENV === "production") { }); } -const PostHogIdentification = ({ children }: { children: React.ReactNode }) => { +const Identification = ({ children }: { children: React.ReactNode }) => { const { data: session } = useSession(); const posthog = usePostHog(); + const user = session?.user; const params = useSearchParams(); const newLoginState = params.get("loginState"); if (newLoginState == "signedIn" && session) { - posthog.identify(session.user.id); + posthog.identify(user?.id); } + useEffect(() => { + Sentry.setUser({ id: user?.id, email: user?.email ?? "" }); + }, [user]); return <>{children}; }; @@ -41,7 +46,7 @@ const Providers = ({ children }: { children: React.ReactNode }) => { - {children} + {children}