fixes server blowing up when a streaming request drops out

This commit is contained in:
nai-degen 2023-05-04 16:37:16 -05:00
parent be1e672e68
commit 4dc9c649d8
1 changed files with 20 additions and 16 deletions

View File

@ -294,27 +294,31 @@ const handleDownstreamErrors: ProxyResHandlerWithBody = async (
throw new Error(errorPayload.error?.message);
};
/** Handles errors in the request rewriter pipeline. */
/** Handles errors in rewriter pipelines. */
export const handleInternalError: httpProxy.ErrorCallback = (
err,
_req,
res
) => {
logger.error({ error: err }, "Error in proxy request pipeline.");
(res as http.ServerResponse).writeHead(500, {
"Content-Type": "application/json",
});
res.end(
JSON.stringify({
error: {
type: "proxy_error",
message: err.message,
stack: err.stack,
proxy_note: `Reverse proxy encountered an error before it could reach the downstream API.`,
},
})
);
logger.error({ error: err }, "Error in http-proxy-middleware pipeline.");
// headers might have already been sent
try {
(res as http.ServerResponse).writeHead(500, {
"Content-Type": "application/json",
});
res.end(
JSON.stringify({
error: {
type: "proxy_error",
message: err.message,
stack: err.stack,
proxy_note: `Reverse proxy encountered an error before it could reach the downstream API.`,
},
})
);
} catch (e) {
logger.error({ error: e }, "Error writing error response headers, giving up.");
}
};
const incrementKeyUsage: ProxyResHandlerWithBody = async (_proxyRes, req) => {