Skip to content

Return 502 instead of 500 when failing to proxy requests in the dev server #21647

@segevfiner

Description

@segevfiner

Description

Instead of returning 500 when the upstream server in not available in the Vite dev server proxying, return 502 (Bad Gateway), which allows to detect this case more cleanly in frontend code and retry.

Suggested solution

Modify:

proxy.on('error', (err, _req, res) => {
// When it is ws proxy, res is net.Socket
if ('req' in res) {
config.logger.error(
`${colors.red(`http proxy error: ${res.req.url}`)}\n${err.stack}`,
{
timestamp: true,
error: err,
},
)
if (!res.headersSent && !res.writableEnded) {
res
.writeHead(500, {
'Content-Type': 'text/plain',
})
.end()
}
} else {
config.logger.error(`${colors.red(`ws proxy error:`)}\n${err.stack}`, {
timestamp: true,
error: err,
})
res.end()
}
})

To return 502 instead of 500, or maybe only for applicable error types.

Alternative

Check for and retry on 500, which is less accurate, and only fits for development mode

Additional context

No response

Validations

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions