Skip to content

Commit a1418dd

Browse files
authored
fix: Handle error for already streaming response gracefully (kyma-project#3805)
1 parent 1d625c7 commit a1418dd

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

backend/companion/companionRouter.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,18 @@ async function handleChatMessage(req, res) {
158158

159159
res.end();
160160
} catch (error) {
161-
res.status(500).json({ error: 'Failed to fetch AI chat data' });
161+
if (!res.headersSent) {
162+
res.status(500).json({ error: 'Failed to fetch AI chat data' });
163+
} else {
164+
setTimeout(() => {
165+
res.write(
166+
JSON.stringify({
167+
error: 'Failed to fetch AI chat data',
168+
}),
169+
);
170+
res.end();
171+
}, 500);
172+
}
162173
}
163174
}
164175

backend/companion/getKcpToken.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function getSecretManagerCredentials() {
7575
.trim(),
7676
};
7777
} catch (error) {
78-
console.warn('Secret Manager credentials could not be read:', error);
78+
console.warn('Secret Manager credentials could not be read');
7979
return null;
8080
}
8181
}

src/components/KymaCompanion/api/getChatResponse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ function readChunk(
9898
}
9999
const receivedString = decoder.decode(value, { stream: true });
100100
const chunk = JSON.parse(receivedString);
101-
if (chunk?.data?.error) {
102-
handleError(chunk.data.error);
103-
return;
101+
// custom error provided by busola backend during streaming, not by companion backend
102+
if (chunk?.error) {
103+
throw new Error(chunk?.error);
104104
}
105105
handleChatResponse(chunk);
106106
readChunk(reader, decoder, handleChatResponse, handleError, sessionID);

0 commit comments

Comments
 (0)