Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43af7c5

Browse files
authored
fix: remove uncaught exception handler (#154)
There are no circumstances under which it is safe to ignore uncaught exceptions, since the application is left in an unconsistent state. The node documentation is unequivocal about this: https://nodejs.org/api/process.html#warning-using-uncaughtexception-correctly Any code that can throw an exception should be wrapped in a `try/catch` or there should be some other method of handling the error.
1 parent 87deb92 commit 43af7c5

File tree

2 files changed

+1
-39
lines changed

2 files changed

+1
-39
lines changed

src/constants.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,3 @@ export const GC_TIMEOUT_MS = 20000
8080
* How long to wait for the healthcheck retrieval of an identity CID to complete
8181
*/
8282
export const HEALTHCHECK_TIMEOUT_MS = 1000
83-
84-
/**
85-
* You can set `RECOVERABLE_ERRORS` to a comma delimited list of errors to recover from.
86-
* If you want to recover from all errors, set `RECOVERABLE_ERRORS` to 'all'.
87-
* If you want to recover from no errors, set `RECOVERABLE_ERRORS` to ''.
88-
*/
89-
export const RECOVERABLE_ERRORS = (() => {
90-
if (process.env.RECOVERABLE_ERRORS === 'all') {
91-
return 'all'
92-
}
93-
if (process.env.RECOVERABLE_ERRORS === '') {
94-
return ''
95-
}
96-
return process.env.RECOVERABLE_ERRORS?.split(',') ?? 'all'
97-
})()
98-
99-
export const ALLOW_UNHANDLED_ERROR_RECOVERY = (() => {
100-
if (RECOVERABLE_ERRORS === 'all') {
101-
return true
102-
}
103-
if (RECOVERABLE_ERRORS === '') {
104-
return false
105-
}
106-
return RECOVERABLE_ERRORS.length > 0
107-
})()

src/index.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
* | `ECHO_HEADERS` | A debug flag to indicate whether you want to output request and response headers | `false` |
6969
* | `USE_DELEGATED_ROUTING` | Whether to use the delegated routing v1 API | `true` |
7070
* | `DELEGATED_ROUTING_V1_HOST` | Hostname to use for delegated routing v1 | `https://delegated-ipfs.dev` |
71-
* | `RECOVERABLE_ERRORS` | A comma delimited list of errors to recover from. These errors are checked in `uncaughtException` and `unhandledRejection` callbacks | `all` |
7271
*
7372
* <!--
7473
* TODO: currently broken when used in docker, but they work when running locally (you can cache datastore and blockstore locally to speed things up if you want)
@@ -161,7 +160,7 @@ import cors from '@fastify/cors'
161160
import { createVerifiedFetch } from '@helia/verified-fetch'
162161
import fastify, { type FastifyInstance, type RouteOptions } from 'fastify'
163162
import metricsPlugin from 'fastify-metrics'
164-
import { HOST, HTTP_PORT, RPC_PORT, METRICS, ECHO_HEADERS, FASTIFY_DEBUG, RECOVERABLE_ERRORS, ALLOW_UNHANDLED_ERROR_RECOVERY } from './constants.js'
163+
import { HOST, HTTP_PORT, RPC_PORT, METRICS, ECHO_HEADERS, FASTIFY_DEBUG } from './constants.js'
165164
import { contentTypeParser } from './content-type-parser.js'
166165
import { getCustomHelia } from './get-custom-helia.js'
167166
import { httpGateway } from './helia-http-gateway.js'
@@ -232,18 +231,6 @@ async function closeGracefully (signal: number | string): Promise<void> {
232231
process.once(signal, closeGracefully)
233232
})
234233

235-
const uncaughtHandler = (error: any): void => {
236-
log.error('Uncaught Exception:', error)
237-
if (ALLOW_UNHANDLED_ERROR_RECOVERY && (RECOVERABLE_ERRORS === 'all' || RECOVERABLE_ERRORS.includes(error?.code) || RECOVERABLE_ERRORS.includes(error?.name))) {
238-
log.trace('Ignoring error')
239-
return
240-
}
241-
void closeGracefully('SIGTERM')
242-
}
243-
244-
process.on('uncaughtException', uncaughtHandler)
245-
process.on('unhandledRejection', uncaughtHandler)
246-
247234
interface ServerOptions {
248235
metrics: boolean
249236
}

0 commit comments

Comments
 (0)