Skip to content

Commit

Permalink
try with more places to log sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ivashchuk committed Mar 29, 2024
1 parent 949b209 commit 89220b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
9 changes: 0 additions & 9 deletions src/app/api/sentry-example-error/route.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/app/api/sentry-example-error/route.ts
Original file line number Diff line number Diff line change
@@ -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..." });
}
13 changes: 9 additions & 4 deletions src/components/providers/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -9,23 +9,28 @@ 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!, {
api_host: env.NEXT_PUBLIC_POSTHOG_HOST!,
});
}

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}</>;
};
Expand All @@ -41,7 +46,7 @@ const Providers = ({ children }: { children: React.ReactNode }) => {
<TRPCReactProvider>
<SessionProvider>
<PostHogProvider client={posthog}>
<PostHogIdentification>{children}</PostHogIdentification>
<Identification>{children}</Identification>
</PostHogProvider>
</SessionProvider>
</TRPCReactProvider>
Expand Down

0 comments on commit 89220b1

Please sign in to comment.