#Enriching error with user content

3 messages · Page 1 of 1 (latest)

low iris
#

I am trying to setup Sentry to catch the user information when there is an error happening. I am using Next.js and T3 Stack, relevant code is below

The logs show the correct info, so user.id exists for sure. When I trigger an api error it pops up correctly on Sentry dashboard, but unfortunately I can't see the user info in tags there

Is there any way for me to debug this/ make sure I am not missing anything?

Thanks for help!

#
import { PrismaAdapter } from "@auth/prisma-adapter";
import {
  getServerSession,
  type DefaultSession,
  type NextAuthOptions,
} from "next-auth";
import { type Adapter } from "next-auth/adapters";
import DiscordProvider from "next-auth/providers/discord";
import * as Sentry from "@sentry/nextjs";

import { env } from "~/env.mjs";
import { db } from "~/server/db";

/**
 * Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
 * object and keep type safety.
 *
 * @see https://next-auth.js.org/getting-started/typescript#module-augmentation
 */
declare module "next-auth" {
  interface Session extends DefaultSession {
    user: {
      id: string;
      // ...other properties
      // role: UserRole;
    } & DefaultSession["user"];
  }

  // interface User {
  //   // ...other properties
  //   // role: UserRole;
  // }
}

/**
 * Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
 *
 * @see https://next-auth.js.org/configuration/options
 */
export const authOptions: NextAuthOptions = {
  callbacks: {
    session: ({ session, user }) => ({
      ...session,
      user: {
        ...session.user,
        id: user.id,
      },
    }),
  },
  events: {
    signIn({ user }) {
      console.log("User signed in, Sentry has registered", user);
      Sentry.setUser({ id: user.id });
    },
    signOut() {
      console.log("User signed out, Sentry has been cleared");
      Sentry.setUser(null);
    },
  },
  adapter: PrismaAdapter(db) as Adapter,
  providers: [
    DiscordProvider({
      clientId: env.DISCORD_CLIENT_ID!,
      clientSecret: env.DISCORD_CLIENT_SECRET!,
    }),
  ],
};


export const getServerAuthSession = () => getServerSession(authOptions);

#
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..." });
}