Skip to content

Commit 9d18f0c

Browse files
authored
Express: handle pipe errors gracefully (#5052)
* handle pipe errors gracefully * move handlers at the top
1 parent 4bbd34c commit 9d18f0c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

utopia-remix/server.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import * as path from 'node:path'
88
import * as url from 'node:url'
99
import sourceMapSupport from 'source-map-support'
1010

11+
// To make sure everything keeps working and the server doesn't crash, if
12+
// there are any uncaught errors, log them out gracefully
13+
process.on('uncaughtException', (err) => {
14+
console.error('server uncaught exception', err)
15+
})
16+
process.on('unhandledRejection', (err) => {
17+
console.error('server unhandled rejection', err)
18+
})
19+
1120
// figlet -f isometric3 'utopia'
1221
const asciiBanner = `
1322
___ ___ ___ ___
@@ -143,7 +152,14 @@ function proxy(originalRequest, originalResponse) {
143152
},
144153
)
145154

146-
originalRequest.pipe(proxyRequest)
155+
originalRequest
156+
.pipe(proxyRequest)
157+
// if the request fails for any non-explicit reason (e.g. ERRCONNREFUSED),
158+
// handle the error gracefully and return a common status code back to the client
159+
.on('error', (err) => {
160+
console.error('failed proxy request', err)
161+
originalResponse.status(502).json({ error: 'Service temporarily unavailable' })
162+
})
147163
}
148164

149165
const corsMiddleware = cors({

0 commit comments

Comments
 (0)