2
0

⬆️ Upgrade sentry and improve its reliability

This commit is contained in:
Baptiste Arnaud
2023-10-07 12:03:42 +02:00
parent 15823df6bd
commit 3e7b9b3afd
23 changed files with 1931 additions and 1726 deletions

View File

@@ -1,6 +1,6 @@
import prisma from '@typebot.io/lib/prisma'
import { authOptions } from '@/pages/api/auth/[...nextauth]'
import { setUser } from '@sentry/nextjs'
import * as Sentry from '@sentry/nextjs'
import { User } from '@typebot.io/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getServerSession } from 'next-auth'
@@ -19,7 +19,7 @@ export const getAuthenticatedUser = async (
| User
| undefined)
if (!user || !('id' in user)) return
setUser({ id: user.id })
Sentry.setUser({ id: user.id })
return user
}
@@ -30,7 +30,7 @@ const authenticateByToken = async (
const user = (await prisma.user.findFirst({
where: { apiTokens: { some: { token: apiToken } } },
})) as User
setUser({ id: user.id })
Sentry.setUser({ id: user.id })
return user
}

View File

@@ -2,11 +2,18 @@ import { TRPCError, initTRPC } from '@trpc/server'
import { Context } from './context'
import { OpenApiMeta } from 'trpc-openapi'
import superjson from 'superjson'
import * as Sentry from '@sentry/nextjs'
const t = initTRPC.context<Context>().meta<OpenApiMeta>().create({
transformer: superjson,
})
const sentryMiddleware = t.middleware(
Sentry.Handlers.trpcMiddleware({
attachRpcInput: true,
})
)
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.user?.id) {
throw new TRPCError({
@@ -20,10 +27,12 @@ const isAuthed = t.middleware(({ next, ctx }) => {
})
})
const finalMiddleware = sentryMiddleware.unstable_pipe(isAuthed)
export const middleware = t.middleware
export const router = t.router
export const publicProcedure = t.procedure
export const publicProcedure = t.procedure.use(sentryMiddleware)
export const authenticatedProcedure = t.procedure.use(isAuthed)
export const authenticatedProcedure = t.procedure.use(finalMiddleware)

View File

@@ -18,6 +18,7 @@ import { Ratelimit } from '@upstash/ratelimit'
import { Redis } from '@upstash/redis/nodejs'
import got from 'got'
import { env } from '@typebot.io/env'
import * as Sentry from '@sentry/nextjs'
const providers: Provider[] = []
@@ -134,6 +135,14 @@ export const authOptions: AuthOptions = {
signIn: '/signin',
newUser: env.NEXT_PUBLIC_ONBOARDING_TYPEBOT_ID ? '/onboarding' : undefined,
},
events: {
signIn({ user }) {
Sentry.setUser({ id: user.id })
},
signOut() {
Sentry.setUser(null)
},
},
callbacks: {
session: async ({ session, user }) => {
const userFromDb = user as User

View File

@@ -1,6 +1,6 @@
import { createContext } from '@/helpers/server/context'
import { trpcRouter } from '@/helpers/server/routers/v1/trpcRouter'
import { captureException } from '@sentry/nextjs'
import * as Sentry from '@sentry/nextjs'
import { createNextApiHandler } from '@trpc/server/adapters/next'
export default createNextApiHandler({
@@ -8,7 +8,7 @@ export default createNextApiHandler({
createContext,
onError({ error }) {
if (error.code === 'INTERNAL_SERVER_ERROR') {
captureException(error)
Sentry.captureException(error)
console.error('Something went wrong', error)
}
return error

View File

@@ -1,6 +1,6 @@
import { createContext } from '@/helpers/server/context'
import { trpcRouter } from '@/helpers/server/routers/v1/trpcRouter'
import { captureException } from '@sentry/nextjs'
import * as Sentry from '@sentry/nextjs'
import { NextApiRequest, NextApiResponse } from 'next'
import { createOpenApiNextHandler } from 'trpc-openapi'
import cors from 'nextjs-cors'
@@ -15,7 +15,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
createContext,
onError({ error }) {
if (error.code === 'INTERNAL_SERVER_ERROR') {
captureException(error)
Sentry.captureException(error)
console.error('Something went wrong', error)
}
},