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
This commit is contained in:
ceruleandeep 2024-11-22 11:51:05 +11:00
parent 0383ea52e9
commit 8de551fc94
3 changed files with 15 additions and 6 deletions

View File

@ -2432,8 +2432,12 @@ async function generatePicture(initiator, args, trigger, message, callback) {
imagePath = await sendGenerationRequest(generationType, prompt, negativePromptPrefix, characterName, callback, initiator, abortController.signal); imagePath = await sendGenerationRequest(generationType, prompt, negativePromptPrefix, characterName, callback, initiator, abortController.signal);
} catch (err) { } catch (err) {
console.trace(err); console.trace(err);
toastr.error('SD prompt text generation failed. Reason: ' + err, 'Image Generation'); // errors here are most likely due to text generation failure
throw new Error('SD prompt text generation failed. Reason: ' + err); // 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 { finally {
$(stopButton).hide(); $(stopButton).hide();

View File

@ -2057,8 +2057,9 @@ async function sendOpenAIRequest(type, messages, signal) {
checkModerationError(data); checkModerationError(data);
if (data.error) { if (data.error) {
toastr.error(data.error.message || response.statusText, t`API returned an error`); const message = data.error.message || response.statusText || t`Unknown error`;
throw new Error(data); toastr.error(message, t`API returned an error`);
throw new Error(message);
} }
if (type !== 'quiet') { if (type !== 'quiet') {

View File

@ -1051,8 +1051,12 @@ router.post('/generate', jsonParser, function (request, response) {
} }
} catch (error) { } catch (error) {
console.log('Generation failed', error); console.log('Generation failed', error);
const message = error.code === 'ECONNREFUSED'
? `Connection refused: ${error.message}`
: error.message || 'Unknown error occurred';
if (!response.headersSent) { if (!response.headersSent) {
response.send({ error: true }); response.status(502).send({ error: { message, ...error } });
} else { } else {
response.end(); response.end();
} }
@ -1068,7 +1072,7 @@ router.post('/generate', jsonParser, function (request, response) {
const message = errorResponse.statusText || 'Unknown error occurred'; const message = errorResponse.statusText || 'Unknown error occurred';
const quota_error = errorResponse.status === 429 && errorData?.error?.type === 'insufficient_quota'; 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) { if (!response.headersSent) {
response.send({ error: { message }, quota_error: quota_error }); response.send({ error: { message }, quota_error: quota_error });