Skip to content

Commit f1f6f66

Browse files
authored
Catch errors that cause shutdowns and send them to Sentry (#873)
1 parent b19ac20 commit f1f6f66

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

index.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ sourceMapSupport.install({
1818
},
1919
})
2020

21-
closeWithGrace(async ({ err }) => {
22-
if (err) {
23-
console.error(chalk.red(err))
24-
console.error(chalk.red(err.stack))
25-
process.exit(1)
26-
}
27-
})
28-
2921
if (process.env.MOCKS === 'true') {
3022
await import('./tests/mocks/index.ts')
3123
}

server/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import crypto from 'node:crypto'
22
import { createRequestHandler } from '@remix-run/express'
33
import { type ServerBuild } from '@remix-run/node'
4+
import Sentry from '@sentry/remix'
45
import { ip as ipAddress } from 'address'
56
import chalk from 'chalk'
67
import closeWithGrace from 'close-with-grace'
@@ -15,8 +16,9 @@ const MODE = process.env.NODE_ENV ?? 'development'
1516
const IS_PROD = MODE === 'production'
1617
const IS_DEV = MODE === 'development'
1718
const ALLOW_INDEXING = process.env.ALLOW_INDEXING !== 'false'
19+
const SENTRY_ENABLED = IS_PROD && process.env.SENTRY_DSN
1820

19-
if (IS_PROD && process.env.SENTRY_DSN) {
21+
if (SENTRY_ENABLED) {
2022
void import('./utils/monitoring.js').then(({ init }) => init())
2123
}
2224

@@ -276,8 +278,16 @@ ${chalk.bold('Press Ctrl+C to stop')}
276278
)
277279
})
278280

279-
closeWithGrace(async () => {
281+
closeWithGrace(async ({ err }) => {
280282
await new Promise((resolve, reject) => {
281283
server.close((e) => (e ? reject(e) : resolve('ok')))
282284
})
285+
if (err) {
286+
console.error(chalk.red(err))
287+
console.error(chalk.red(err.stack))
288+
if (SENTRY_ENABLED) {
289+
Sentry.captureException(err)
290+
await Sentry.flush(500)
291+
}
292+
}
283293
})

0 commit comments

Comments
 (0)