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

@ -2,7 +2,6 @@ import { IncomingMessage } from 'http'
import { ErrorPage } from '@/components/ErrorPage'
import { NotFoundPage } from '@/components/NotFoundPage'
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import sanitizeHtml from 'sanitize-html'
import { env, getViewerUrl, isDefined, isNotDefined, omit } from 'utils'
import { TypebotPage, TypebotPageProps } from '../components/TypebotPage'
import prisma from '../lib/prisma'
@ -46,14 +45,7 @@ export const getServerSideProps: GetServerSideProps = async (
isIE,
url: `https://${forwardedHost ?? host}${pathname}`,
customHeadCode:
isDefined(headCode) && headCode !== ''
? sanitizeHtml(headCode, {
allowedTags: ['script', 'meta'],
allowedAttributes: {
meta: ['name', 'content'],
},
})
: null,
isDefined(headCode) && headCode !== '' ? headCode : null,
},
}
} catch (err) {

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

@ -16,7 +16,6 @@ import {
LogicalOperator,
} from 'models'
import Cors from 'cors'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedGoogleClient } from '@/lib/google-sheets'
import { saveErrorLog, saveSuccessLog } from '@/features/logs/api'
@ -237,4 +236,4 @@ const getExtractingColumns = (columns: string | string[] | undefined) => {
if (Array.isArray(columns)) return columns
}
export default withSentry(handler)
export default handler

View File

@ -9,7 +9,6 @@ import {
import Stripe from 'stripe'
import Cors from 'cors'
import { withSentry } from '@sentry/nextjs'
import { PaymentInputOptions, StripeCredentialsData, Variable } from 'models'
import prisma from '@/lib/prisma'
import { parseVariables } from 'bot-engine'
@ -133,4 +132,4 @@ const isZeroDecimalCurrency = (currency: string) =>
'XPF',
].includes(currency)
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'
@ -12,4 +11,4 @@ const handler = (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 { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils/api'
@ -12,4 +11,4 @@ const handler = (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 Cors from 'cors'
@ -19,4 +18,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

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

View File

@ -17,7 +17,6 @@ import got, { Method, Headers, HTTPError } from 'got'
import { byId, omit, parseAnswers } from 'utils'
import { initMiddleware, methodNotAllowed, notFound } from 'utils/api'
import { stringify } from 'qs'
import { withSentry } from '@sentry/nextjs'
import Cors from 'cors'
import prisma from '@/lib/prisma'
import { saveErrorLog, saveSuccessLog } from '@/features/logs/api'
@ -241,4 +240,4 @@ const safeJsonParse = (json: string): { data: any; isJson: boolean } => {
}
}
export default withSentry(handler)
export default handler

View File

@ -11,7 +11,6 @@ import {
import { NextApiRequest, NextApiResponse } from 'next'
import { initMiddleware, methodNotAllowed, notFound } from 'utils/api'
import { byId } from 'utils'
import { withSentry } from '@sentry/nextjs'
import Cors from 'cors'
import { executeWebhook } from '../../executeWebhook'
@ -68,4 +67,4 @@ const prepareWebhookAttributes = (
return webhook
}
export default withSentry(handler)
export default handler

View File

@ -1,4 +1,3 @@
import { withSentry } from '@sentry/nextjs'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils/api'
@ -44,4 +43,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 { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils/api'
@ -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 prisma from '@/lib/prisma'
import { InputBlockType, PublicTypebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
@ -164,4 +163,4 @@ const sendReachStorageLimitNotification = async ({
})
}
export default withSentry(handler)
export default handler

View File

@ -1,6 +1,5 @@
import { authenticateUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { withSentry } from '@sentry/nextjs'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { byId } from 'utils'
@ -43,4 +42,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,6 +1,5 @@
import { authenticateUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { withSentry } from '@sentry/nextjs'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { byId } from 'utils'
@ -38,4 +37,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -11,7 +11,6 @@ import { methodNotAllowed, initMiddleware, decrypt } from 'utils/api'
import { saveErrorLog, saveSuccessLog } from '@/features/logs/api'
import Cors from 'cors'
import { withSentry } from '@sentry/nextjs'
import Mail from 'nodemailer/lib/mailer'
import { DefaultBotNotificationEmail } from 'emails'
import { render } from '@faire/mjml-react/dist/src/utils/render'
@ -218,4 +217,4 @@ const parseEmailRecipient = (
}
}
export default withSentry(handler)
export default handler

View File

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

View File

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

View File

@ -1,6 +1,5 @@
import { authenticateUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { withSentry } from '@sentry/nextjs'
import { Group, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { byId, isWebhookBlock } from 'utils'
@ -38,4 +37,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,6 +1,5 @@
import { authenticateUser } from '@/features/auth/api'
import prisma from '@/lib/prisma'
import { withSentry } from '@sentry/nextjs'
import { Group, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { byId, isNotDefined, isWebhookBlock } from 'utils'
@ -42,4 +41,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler

View File

@ -1,5 +1,4 @@
import { authenticateUser } from '@/features/auth/api'
import { withSentry } from '@sentry/nextjs'
import { NextApiRequest, NextApiResponse } from 'next'
import { isNotDefined } from 'utils'
import { methodNotAllowed } from 'utils/api'
@ -14,4 +13,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
export default withSentry(handler)
export default handler