I’m building a chatbot client using the Vue3 composition api. I have a fastapi backend post endpoint that returns a StreamingResponse. On the UI, I use a fetch request using the getReader method: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader
try {
fetch('api...', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: jsonPrompt
})
.then(response => {
if (!response.ok || !response.body) {
return;
}
const reader = response.body.getReader();
reader.read().then(function processText({ done, value }) {
if (done) {
console.log('Stream done');
return;
}
const decodedData = new TextDecoder('utf-8').decode(value);
console.log('decodedData:', decodedData);
//processStream(decodedData);
return reader.read().then(processText);
})
})
.catch(error => {
console.error('Error in promise:', error);
});
}
catch (error) {
console.error('Error:', error);
}
Occasionally, I get a TypeError: network error, after the stream has started, but before the done flag returns true, with the response status 200!? net::ERR_HTTP2_PROTOCOL_ERROR 200 (OK)
And the try catch
I added around the fetch does not catch this error:
Any idea how I can catch this error during processing of chunks from getReader?