From 8de551fc9405984acd003667d2cf25d4c8887925 Mon Sep 17 00:00:00 2001 From: ceruleandeep Date: Fri, 22 Nov 2024 11:51:05 +1100 Subject: [PATCH] Return 502 with error description when connection to remote CC API fails If chat-completions/generate returns an error, throw the error message Reformat display of exceptions during SD prompt text generation --- public/scripts/extensions/stable-diffusion/index.js | 8 ++++++-- public/scripts/openai.js | 5 +++-- src/endpoints/backends/chat-completions.js | 8 ++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/public/scripts/extensions/stable-diffusion/index.js b/public/scripts/extensions/stable-diffusion/index.js index 2c6c00f58..817951f39 100644 --- a/public/scripts/extensions/stable-diffusion/index.js +++ b/public/scripts/extensions/stable-diffusion/index.js @@ -2432,8 +2432,12 @@ async function generatePicture(initiator, args, trigger, message, callback) { imagePath = await sendGenerationRequest(generationType, prompt, negativePromptPrefix, characterName, callback, initiator, abortController.signal); } catch (err) { console.trace(err); - toastr.error('SD prompt text generation failed. Reason: ' + err, 'Image Generation'); - throw new Error('SD prompt text generation failed. Reason: ' + err); + // errors here are most likely due to text generation failure + // sendGenerationRequest mostly deals with its own errors + const reason = err.error?.message || err.message || 'Unknown error'; + const errorText = 'SD prompt text generation failed. ' + reason; + toastr.error(errorText, 'Image Generation'); + throw new Error(errorText); } finally { $(stopButton).hide(); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 8e76373b2..a63a7d8ee 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -2057,8 +2057,9 @@ async function sendOpenAIRequest(type, messages, signal) { checkModerationError(data); if (data.error) { - toastr.error(data.error.message || response.statusText, t`API returned an error`); - throw new Error(data); + const message = data.error.message || response.statusText || t`Unknown error`; + toastr.error(message, t`API returned an error`); + throw new Error(message); } if (type !== 'quiet') { diff --git a/src/endpoints/backends/chat-completions.js b/src/endpoints/backends/chat-completions.js index 578b0a677..2cd05e5a1 100644 --- a/src/endpoints/backends/chat-completions.js +++ b/src/endpoints/backends/chat-completions.js @@ -1051,8 +1051,12 @@ router.post('/generate', jsonParser, function (request, response) { } } catch (error) { console.log('Generation failed', error); + const message = error.code === 'ECONNREFUSED' + ? `Connection refused: ${error.message}` + : error.message || 'Unknown error occurred'; + if (!response.headersSent) { - response.send({ error: true }); + response.status(502).send({ error: { message, ...error } }); } else { response.end(); } @@ -1068,7 +1072,7 @@ router.post('/generate', jsonParser, function (request, response) { const message = errorResponse.statusText || 'Unknown error occurred'; const quota_error = errorResponse.status === 429 && errorData?.error?.type === 'insufficient_quota'; - console.log(message, responseText); + console.log('Chat completion request error: ', message, responseText); if (!response.headersSent) { response.send({ error: { message }, quota_error: quota_error });