EventSource
EventSource copied to clipboard
Unhandled rejection when connection to eventsource gets terminated
I am using the event-source-polyfill library to create an EventSource connection in my Nodejs project using the dynamic imports mentioned in docs.
The connection to the eventsource server gets terminated occasionally due to unknown reasons.
Expected behaviour:
Eventsource catches the error caused by connection termination and handles it as required
Actual behaviour:
The error thrown due to this termination is not caught, leading to an unhandlerejection
in the node process. The error stack I obtain is:
TypeError: terminated
at Fetch.onAborted (node:internal/deps/undici/undici:14036:53)
at Fetch.emit (node:events:513:28)
at Fetch.emit (node:domain:489:12)
at Fetch.terminate (node:internal/deps/undici/undici:13295:14)
at Object.onError (node:internal/deps/undici/undici:14131:36)
at Request.onError (node:internal/deps/undici/undici:7601:31)
at errorRequest (node:internal/deps/undici/undici:9797:17)
at TLSSocket.onSocketClose (node:internal/deps/undici/undici:9248:9)
at TLSSocket.emit (node:events:525:35)
at TLSSocket.emit (node:domain:489:12) {
[cause]: BodyTimeoutError: Body Timeout Error
at Timeout.onParserTimeout [as _onTimeout] (node:internal/deps/undici/undici:9189:32)
at listOnTimeout (node:internal/timers:571:11)
at process.processTimers (node:internal/timers:512:7) {
code: 'UND_ERR_BODY_TIMEOUT'
}
}
Additional details
The EventSource is initialized by:
import('@titelmedia/node-fetch').then(function (fetch) {
globalThis.fetch = fetch.default;
globalThis.Response = fetch.default.Response;
const source = new EventSource(url);
callback(null, source);
});
The error is thrown from time-to-time when the eventsource times out and creates a new connection.
We are experiencing a similar issue when using this library with React Native.
Is there an answer to that question now?
Bump
You are saying it is "connection termination", but really the line of code of XMLHttpRequest.js looks like something else. Is it a bug of that lib? The attached stack trace does look relevant to EventSorce library. Is the exception really not caught?
You are saying it is "connection termination"
I am not sure what is happening in my case but even with a proper internet connection, the request throws this exception with the error message of "TypeError: terminated" with the cause always as UND_ERR_BODY_TIMEOUT
. And it happens every 3-5 minutes.
If there is anything I can do to get clearer error messages or more info, please do suggest them.