Skip to content

Commit

Permalink
Catch errors that cause shutdowns and send them to Sentry (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw authored Oct 10, 2024
1 parent b19ac20 commit f1f6f66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 0 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ sourceMapSupport.install({
},
})

closeWithGrace(async ({ err }) => {
if (err) {
console.error(chalk.red(err))
console.error(chalk.red(err.stack))
process.exit(1)
}
})

if (process.env.MOCKS === 'true') {
await import('./tests/mocks/index.ts')
}
Expand Down
14 changes: 12 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'node:crypto'
import { createRequestHandler } from '@remix-run/express'
import { type ServerBuild } from '@remix-run/node'
import Sentry from '@sentry/remix'
import { ip as ipAddress } from 'address'
import chalk from 'chalk'
import closeWithGrace from 'close-with-grace'
Expand All @@ -15,8 +16,9 @@ const MODE = process.env.NODE_ENV ?? 'development'
const IS_PROD = MODE === 'production'
const IS_DEV = MODE === 'development'
const ALLOW_INDEXING = process.env.ALLOW_INDEXING !== 'false'
const SENTRY_ENABLED = IS_PROD && process.env.SENTRY_DSN

if (IS_PROD && process.env.SENTRY_DSN) {
if (SENTRY_ENABLED) {
void import('./utils/monitoring.js').then(({ init }) => init())
}

Expand Down Expand Up @@ -276,8 +278,16 @@ ${chalk.bold('Press Ctrl+C to stop')}
)
})

closeWithGrace(async () => {
closeWithGrace(async ({ err }) => {
await new Promise((resolve, reject) => {
server.close((e) => (e ? reject(e) : resolve('ok')))
})
if (err) {
console.error(chalk.red(err))
console.error(chalk.red(err.stack))
if (SENTRY_ENABLED) {
Sentry.captureException(err)
await Sentry.flush(500)
}
}
})

0 comments on commit f1f6f66

Please sign in to comment.