Skip to content

Commit

Permalink
Merge pull request #17 from AllanOliveiraM/develop
Browse files Browse the repository at this point in the history
feat: use nextjs sentry lib
  • Loading branch information
AllanOliveiraM authored Jul 17, 2021
2 parents 36b7147 + 83d2db9 commit 606b3c1
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 75 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"@hookform/resolvers": "^2.6.0",
"@mdx-js/loader": "^1.6.22",
"@next/mdx": "^11.0.1",
"@sentry/react": "^6.8.0",
"@sentry/tracing": "^6.8.0",
"@sentry/nextjs": "^6.9.0",
"@welldone-software/why-did-you-render": "^6.2.0",
"@xstyled/styled-components": "^3.0.3",
"@xstyled/system": "^3.0.2",
Expand Down
3 changes: 1 addition & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AppProps as NextAppProps } from 'next/app'

import { Preflight } from '@xstyled/styled-components'
Expand All @@ -16,7 +15,7 @@ import 'sanitize.css/typography.css'

initSentry()
interface AppProps extends NextAppProps {
err: any
err: unknown
}

// Workaround for https://github.com/vercel/next.js/issues/8592
Expand Down
32 changes: 32 additions & 0 deletions src/pages/cats.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type HomeProps = { catFacts: string[] | undefined }

const Home = ({ catFacts }: HomeProps) => (
<div>
<ul>
{catFacts?.map(catFact => (
<li key={catFact}>Fact: {catFact}</li>
))}
</ul>
</div>
)

export const getStaticProps = async () => {
// const response = await fetch('https://cat-fact.herokuapp.com/facts')
// const parsedData = await response.json()

// const formattedData = parsedData?.map((cat) => cat.text)

const formattedData = [
'Cats make about 100 different sounds. Dogs make only about 10.',
'Domestic cats spend about 70 percent of the day sleeping and 15 percent of the day grooming.',
'I do not know anything about cats.',
'The technical term for a cat’s hairball is a bezoar.',
'Cats are the most popular pet in the United States.',
]

return {
props: { catFacts: formattedData },
}
}

export default Home
39 changes: 20 additions & 19 deletions src/utils/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import * as SentryEngine from '@sentry/react'
import { Integrations } from '@sentry/tracing'
/* eslint-disable no-console */
import {
init,
captureException as sentryCaptureException,
flush,
Exception,
} from '@sentry/nextjs'

import { getPublicEnvironmentConfig, isProductionMode } from 'utils/environment'

const { NEXT_PUBLIC_SENTRY_DSN } = getPublicEnvironmentConfig()

export const initSentry = () => {
if (!isProductionMode()) {
// eslint-disable-next-line no-console
console.debug('[MONITORING] Sentry skipped.')

return
}

if (!NEXT_PUBLIC_SENTRY_DSN) {
// eslint-disable-next-line no-console
console.error(
'[MONITORING] Sentry not loaded. A valid DSN config could not be found.'
)
Expand All @@ -23,48 +26,46 @@ export const initSentry = () => {
}

try {
SentryEngine.init({
init({
enabled: isProductionMode(),
integrations: [new Integrations.BrowserTracing()],
dsn: NEXT_PUBLIC_SENTRY_DSN,
})
} catch (error) {
// eslint-disable-next-line no-console
console.error(
'[MONITORING] Sentry not loaded. An error was encountered when starting Sentry.'
'[MONITORING] Sentry not loaded. An error was encountered when starting Sentry.',
error
)
}
}

export const captureException = async (error: Error) => {
export const captureException = async (error: Error | Exception) => {
if (isProductionMode()) {
try {
SentryEngine.captureException(error)
sentryCaptureException(error)

const success = await SentryEngine.flush(5000)
const success = await flush(5000)

if (!success) {
throw new Error()
throw new Error(
`Flush pass: false. It may not have been possible to send exception data to Nexpy servers. Please report this bug! ${String(
error
)}`
)
}

// eslint-disable-next-line no-console
console.error(
`Flush pass: ${success}`,
'This exception was caught automatically and will be debugged.',
'Your correction will be evaluated and contact support is not necessary.',
error
)
} catch {
// eslint-disable-next-line no-console
console.error(
'[MONITORING] It was not possible to send exception data for Nexpy servers. Please report this bug!',
'[MONITORING] It was not possible to send exception data for Nexpy servers. Please report this bug! ',
error
)
}
} else {
// eslint-disable-next-line no-console
console.error('[TRACKED_ERROR] ', error)
console.error('[DEV_ERROR] ', error)
}
}

export default SentryEngine
Loading

1 comment on commit 606b3c1

@vercel
Copy link

@vercel vercel bot commented on 606b3c1 Jul 17, 2021

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.