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