From 57fd17ede0a88856a7031b8c350fd4c362120b47 Mon Sep 17 00:00:00 2001 From: nai-degen Date: Mon, 27 May 2024 15:30:28 -0500 Subject: [PATCH] makes it easier for clients to detect proxy errors programatically --- src/proxy/middleware/response/error-generator.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/proxy/middleware/response/error-generator.ts b/src/proxy/middleware/response/error-generator.ts index d2ba152..c5c9735 100644 --- a/src/proxy/middleware/response/error-generator.ts +++ b/src/proxy/middleware/response/error-generator.ts @@ -52,7 +52,13 @@ function getMessageContent({ delete obj.stack; } - return [header, friendlyMessage, serializedObj, prettyTrace].join("\n\n"); + return [ + header, + friendlyMessage, + serializedObj, + prettyTrace, + "", + ].join("\n\n"); } type ErrorGeneratorOptions = { @@ -116,6 +122,11 @@ export function sendErrorToClient({ const isStreaming = req.isStreaming || req.body.stream === true || req.body.stream === "true"; + if (!res.headersSent) { + res.setHeader("x-oai-proxy-error", options.title); + res.setHeader("x-oai-proxy-error-status", options.statusCode || 500); + } + if (isStreaming) { if (!res.headersSent) { initializeSseStream(res);