Skip to content

Commit

Permalink
Express: handle pipe errors gracefully (#5052)
Browse files Browse the repository at this point in the history
* handle pipe errors gracefully

* move handlers at the top
  • Loading branch information
ruggi authored Mar 15, 2024
1 parent 4bbd34c commit 9d18f0c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion utopia-remix/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import * as path from 'node:path'
import * as url from 'node:url'
import sourceMapSupport from 'source-map-support'

// To make sure everything keeps working and the server doesn't crash, if
// there are any uncaught errors, log them out gracefully
process.on('uncaughtException', (err) => {
console.error('server uncaught exception', err)
})
process.on('unhandledRejection', (err) => {
console.error('server unhandled rejection', err)
})

// figlet -f isometric3 'utopia'
const asciiBanner = `
___ ___ ___ ___
Expand Down Expand Up @@ -143,7 +152,14 @@ function proxy(originalRequest, originalResponse) {
},
)

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

const corsMiddleware = cors({
Expand Down

0 comments on commit 9d18f0c

Please sign in to comment.