Fix streaming processor error handler hooks

This commit is contained in:
Cohee
2023-12-08 02:01:08 +02:00
parent 055d6c4337
commit b0e7b73a32
6 changed files with 42 additions and 25 deletions

View File

@@ -163,7 +163,7 @@ function tryParseStreamingError(response, decoded) {
}
if (data.error) {
toastr.error(data.error.message || response.statusText, 'API returned an error');
toastr.error(data.error.message || response.statusText, 'KoboldAI API');
throw new Error(data);
}
}
@@ -180,7 +180,7 @@ export async function generateKoboldWithStreaming(generate_data, signal) {
signal: signal,
});
if (!response.ok) {
tryParseStreamingError(response, await response.body.text());
tryParseStreamingError(response, await response.text());
throw new Error(`Got response status ${response.status}`);
}
const eventStream = new EventSourceStream();

View File

@@ -672,8 +672,8 @@ function tryParseStreamingError(response, decoded) {
return;
}
if (data.error) {
toastr.error(data.error.message || response.statusText, 'API returned an error');
if (data.message || data.error) {
toastr.error(data.message || data.error?.message || response.statusText, 'NovelAI API');
throw new Error(data);
}
}
@@ -692,7 +692,7 @@ export async function generateNovelWithStreaming(generate_data, signal) {
signal: signal,
});
if (!response.ok) {
tryParseStreamingError(response, await response.body.text());
tryParseStreamingError(response, await response.text());
throw new Error(`Got response status ${response.status}`);
}
const eventStream = new EventSourceStream();

View File

@@ -1123,7 +1123,7 @@ function tryParseStreamingError(response, decoded) {
checkQuotaError(data);
if (data.error) {
toastr.error(data.error.message || response.statusText, 'API returned an error');
toastr.error(data.error.message || response.statusText, 'Chat Completion API');
throw new Error(data);
}
}
@@ -1564,7 +1564,7 @@ async function sendOpenAIRequest(type, messages, signal) {
});
if (!response.ok) {
tryParseStreamingError(response, await response.body.text());
tryParseStreamingError(response, await response.text());
throw new Error(`Got response status ${response.status}`);
}

View File

@@ -478,7 +478,7 @@ async function generateTextGenWithStreaming(generate_data, signal) {
});
if (!response.ok) {
tryParseStreamingError(response, await response.body.text());
tryParseStreamingError(response, await response.text());
throw new Error(`Got response status ${response.status}`);
}
@@ -512,14 +512,15 @@ async function generateTextGenWithStreaming(generate_data, signal) {
/**
* Parses errors in streaming responses and displays them in toastr.
* @param {string} response - Response from the server.
* @param {Response} response - Response from the server.
* @param {string} decoded - Decoded response body.
* @returns {void} Nothing.
*/
function tryParseStreamingError(response) {
function tryParseStreamingError(response, decoded) {
let data = {};
try {
data = JSON.parse(response);
data = JSON.parse(decoded);
} catch {
// No JSON. Do nothing.
}
@@ -527,7 +528,7 @@ function tryParseStreamingError(response) {
const message = data?.error?.message || data?.message;
if (message) {
toastr.error(message, 'API Error');
toastr.error(message, 'Text Completion API');
throw new Error(message);
}
}