2
0

💚 Fix sentry not receiving events

This commit is contained in:
Baptiste Arnaud
2022-12-16 08:39:14 +01:00
parent 92dc797b6c
commit 68de7b720f
75 changed files with 194 additions and 332 deletions

View File

@ -1,76 +1,43 @@
import NextErrorComponent, { ErrorProps } from 'next/error'
/**
* NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher.
*
* NOTE: If using this with `next` version 12.2.0 or lower, uncomment the
* penultimate line in `CustomErrorComponent`.
*
* This page is loaded by Nextjs:
* - on the server, when data-fetching methods throw or reject
* - on the client, when `getInitialProps` throws or rejects
* - on the client, when a React lifecycle method throws or rejects, and it's
* caught by the built-in Nextjs error boundary
*
* See:
* - https://nextjs.org/docs/basic-features/data-fetching/overview
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
* - https://reactjs.org/docs/error-boundaries.html
*/
import * as Sentry from '@sentry/nextjs'
import { NextPageContext } from 'next'
import NextErrorComponent from 'next/error'
const MyError = ({
statusCode,
hasGetInitialPropsRun,
err,
}: {
const CustomErrorComponent = (props: {
statusCode: number
hasGetInitialPropsRun: boolean
err: Error
}) => {
if (!hasGetInitialPropsRun && err) {
// getInitialProps is not called in case of
// https://github.com/vercel/next.js/issues/8592. As a workaround, we pass
// err via _app.js so it can be captured
Sentry.captureException(err)
// Flushing is not required in this case as it only happens on the client
}
// If you're using a Nextjs version prior to 12.2.1, uncomment this to
// compensate for https://github.com/vercel/next.js/issues/8592
// Sentry.captureUnderscoreErrorException(props);
return <NextErrorComponent statusCode={statusCode} />
return <NextErrorComponent statusCode={props.statusCode} />
}
MyError.getInitialProps = async (context: NextPageContext) => {
const errorInitialProps = (await NextErrorComponent.getInitialProps(
context
)) as ErrorProps & { hasGetInitialPropsRun: boolean }
CustomErrorComponent.getInitialProps = async (contextData: any) => {
// In case this is running in a serverless function, await this in order to give Sentry
// time to send the error before the lambda exits
await Sentry.captureUnderscoreErrorException(contextData)
const { res, err, asPath } = context
// Workaround for https://github.com/vercel/next.js/issues/8592, mark when
// getInitialProps has run
errorInitialProps.hasGetInitialPropsRun = true
// Returning early because we don't want to log 404 errors to Sentry.
if (res?.statusCode === 404) {
return errorInitialProps
}
// Running on the server, the response object (`res`) is available.
//
// Next.js will pass an err on the server if a page's data fetching methods
// threw or returned a Promise that rejected
//
// Running on the client (browser), Next.js will provide an err if:
//
// - a page's `getInitialProps` threw or returned a Promise that rejected
// - an exception was thrown somewhere in the React lifecycle (render,
// componentDidMount, etc) that was caught by Next.js's React Error
// Boundary. Read more about what types of exceptions are caught by Error
// Boundaries: https://reactjs.org/docs/error-boundaries.html
if (err) {
Sentry.captureException(err)
// Flushing before returning is necessary if deploying to Vercel, see
// https://vercel.com/docs/platform/limits#streaming-responses
await Sentry.flush(2000)
return errorInitialProps
}
// If this point is reached, getInitialProps was called without any
// information about what the error might be. This is unexpected and may
// indicate a bug introduced in Next.js, so record it in Sentry
Sentry.captureException(
new Error(`_error.js getInitialProps missing data at path: ${asPath}`)
)
await Sentry.flush(2000)
return errorInitialProps
// This will contain the status code of the response
return NextErrorComponent.getInitialProps(contextData)
}
export default MyError
export default CustomErrorComponent

View File

@ -8,7 +8,6 @@ import AzureADProvider from 'next-auth/providers/azure-ad'
import prisma from '@/lib/prisma'
import { Provider } from 'next-auth/providers'
import { NextApiRequest, NextApiResponse } from 'next'
import { withSentry } from '@sentry/nextjs'
import { CustomAdapter } from './adapter'
import { User } from 'db'
import { env, isNotEmpty } from 'utils'
@ -194,4 +193,4 @@ const getRequiredGroups = (provider: string): string[] => {
const checkHasGroups = (userGroups: string[], requiredGroups: string[]) =>
userGroups?.some((userGroup) => requiredGroups?.includes(userGroup))
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { Credentials } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
@ -48,4 +47,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
@ -22,4 +21,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -6,7 +6,6 @@ import { stringify } from 'querystring'
import { CredentialsType } from 'models'
import { badRequest, encrypt, notAuthenticated } from 'utils/api'
import { oauth2Client } from '@/lib/googleSheets'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from '@/features/auth/api'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
@ -57,4 +56,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { oauth2Client } from '@/lib/googleSheets'
import { NextApiRequest, NextApiResponse } from 'next'
@ -20,4 +19,4 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
}
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { CustomDomain } from 'db'
import { got, HTTPError } from 'got'
import prisma from '@/lib/prisma'
@ -58,4 +57,4 @@ const createDomainOnVercel = (name: string) =>
json: { name },
})
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils/api'
@ -30,4 +29,4 @@ const deleteDomainOnVercel = (name: string) =>
headers: { Authorization: `Bearer ${process.env.VERCEL_TOKEN}` },
})
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { DashboardFolder, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -51,4 +50,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { DashboardFolder } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -41,4 +40,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -37,4 +37,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
}
export default withSentry(handler)
export default handler

View File

@ -28,4 +28,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -46,4 +46,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils/api'
@ -9,4 +8,4 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils/api'
@ -24,4 +23,4 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils/api'
@ -16,4 +15,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { InputBlockType, PublicTypebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
@ -39,4 +38,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { InputBlockType, PublicTypebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
@ -47,4 +46,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
import {
@ -36,4 +35,4 @@ const handler = async (
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -6,7 +6,6 @@ import {
notAuthenticated,
} from 'utils/api'
import Stripe from 'stripe'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { WorkspaceRole } from 'db'
@ -39,4 +38,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { Plan } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -58,4 +57,4 @@ const createCheckoutSession = async (userId: string) => {
})
}
export default withSentry(handler)
export default handler

View File

@ -6,7 +6,6 @@ import {
notAuthenticated,
} from 'utils/api'
import Stripe from 'stripe'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { WorkspaceRole } from 'db'
@ -46,4 +45,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -7,7 +7,6 @@ import {
notAuthenticated,
} from 'utils/api'
import Stripe from 'stripe'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { Plan, WorkspaceRole } from 'db'
@ -258,4 +257,4 @@ const parseSubscriptionItems = (
: []
)
export default withSentry(handler)
export default handler

View File

@ -4,7 +4,6 @@ import Stripe from 'stripe'
import Cors from 'micro-cors'
import { buffer } from 'micro'
import prisma from '@/lib/prisma'
import { withSentry } from '@sentry/nextjs'
import { Plan } from 'db'
if (!process.env.STRIPE_SECRET_KEY || !process.env.STRIPE_WEBHOOK_SECRET)
@ -128,4 +127,4 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(cors(webhookHandler as any))
export default cors(webhookHandler as any)

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { Plan, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -111,4 +110,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { CollaborationType } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -87,4 +86,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -2,7 +2,6 @@ import { PublicTypebot } from 'models'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed, notAuthenticated } from 'utils/api'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from '@/features/auth/api'
import { canReadTypebots } from '@/utils/api/dbRules'
@ -35,4 +34,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { Stats } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
@ -47,4 +46,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebots } from '@/utils/api/dbRules'
@ -20,4 +19,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebots } from '@/utils/api/dbRules'
@ -21,4 +20,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canEditGuests } from '@/utils/api/dbRules'
@ -31,4 +30,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { CollaborationType, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -82,4 +81,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { Invitation } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -35,4 +34,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { defaultWebhookAttributes } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
@ -24,4 +23,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
@ -20,4 +19,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
@ -37,4 +36,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
@ -18,4 +17,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { CollaborationType } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -68,4 +67,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { Workspace, WorkspaceInvitation, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -74,4 +73,4 @@ const checkIfSeatsLimitReached = async (workspace: Workspace) => {
return existingMembersCount >= getSeatsLimit(workspace)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { WorkspaceInvitation, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -36,4 +35,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
@ -39,4 +38,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { MemberInWorkspace, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -45,4 +44,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
@ -60,4 +59,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler