Skip to content

Commit

Permalink
🤖 sentry (#273)
Browse files Browse the repository at this point in the history
* install and configure sentry.

* fake errors.

* move to environment variables.

* enable debug.

* make env variable public.

* add log messages to api routes.

* move logs inside handler.

* cleanup.

* remove error button.

Co-authored-by: Brad Garropy <[email protected]>
  • Loading branch information
bradgarropy and bgarropy-atlassian authored Feb 2, 2022
1 parent cfec2c3 commit 11bd772
Show file tree
Hide file tree
Showing 9 changed files with 1,815 additions and 139 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ coverage

# logs
debug.log

10 changes: 9 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
const {withSentryConfig} = require("@sentry/nextjs")

const config = {
i18n: {
locales: ["en-US"],
defaultLocale: "en-US",
Expand Down Expand Up @@ -94,3 +96,9 @@ module.exports = {
return config
},
}

const sentryWebpackPluginOptions = {
silent: true,
}

module.exports = withSentryConfig(config, sentryWebpackPluginOptions)
1,878 changes: 1,746 additions & 132 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@fortawesome/react-fontawesome": "^0.1.16",
"@octokit/graphql": "^4.8.0",
"@remark-embedder/core": "^2.0.0",
"@sentry/nextjs": "^6.17.3",
"classnames": "^2.3.1",
"date-fns": "^2.27.0",
"form-data": "^4.0.0",
Expand Down
6 changes: 6 additions & 0 deletions sentry.client.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as Sentry from "@sentry/nextjs"

Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
tracesSampleRate: 1.0,
})
6 changes: 6 additions & 0 deletions sentry.server.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as Sentry from "@sentry/nextjs"

Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
tracesSampleRate: 1.0,
})
38 changes: 38 additions & 0 deletions src/pages/_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as Sentry from "@sentry/nextjs"
import NextErrorComponent from "next/error"

const MyError = ({statusCode, hasGetInitialPropsRun, err}) => {
if (!hasGetInitialPropsRun && err) {
Sentry.captureException(err)
}

return <NextErrorComponent statusCode={statusCode} />
}

MyError.getInitialProps = async context => {
const errorInitialProps = await NextErrorComponent.getInitialProps(context)
const {res, err, asPath} = context

errorInitialProps.hasGetInitialPropsRun = true

if (res?.statusCode === 404) {
return errorInitialProps
}

if (err) {
Sentry.captureException(err)
await Sentry.flush(2000)

return errorInitialProps
}

Sentry.captureException(
new Error(`_error.js getInitialProps missing data at path: ${asPath}`),
)

await Sentry.flush(2000)

return errorInitialProps
}

export default MyError
7 changes: 4 additions & 3 deletions api/subscribe.ts → src/pages/api/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {post} from "@bradgarropy/http"
import {VercelRequest, VercelResponse} from "@vercel/node"
import {withSentry} from "@sentry/nextjs"
import {NextApiHandler} from "next"

const handler = async (req: VercelRequest, res: VercelResponse) => {
const handler: NextApiHandler = async (req, res) => {
const apiKey = process.env.REVUE_API_KEY

const subscriber = await post(
Expand All @@ -20,4 +21,4 @@ const handler = async (req: VercelRequest, res: VercelResponse) => {
res.status(200).json(subscriber)
}

export default handler
export default withSentry(handler)
7 changes: 4 additions & 3 deletions api/twitch.ts → src/pages/api/twitch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {get, post} from "@bradgarropy/http"
import {VercelRequest, VercelResponse} from "@vercel/node"
import {withSentry} from "@sentry/nextjs"
import {NextApiHandler} from "next"

type ChannelStatus = {
isLive: boolean
}

const handler = async (req: VercelRequest, res: VercelResponse) => {
const handler: NextApiHandler = async (req, res) => {
const clientId = process.env.TWITCH_CLIENT_ID
const clientSecret = process.env.TWITCH_CLIENT_SECRET

Expand All @@ -32,4 +33,4 @@ const handler = async (req: VercelRequest, res: VercelResponse) => {
res.status(200).json(channelStatus)
}

export default handler
export default withSentry(handler)

1 comment on commit 11bd772

@vercel
Copy link

@vercel vercel bot commented on 11bd772 Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.