Skip to content

Commit 0485ec9

Browse files
committed
Unify errors handling in the http request
Only once call abort of the request Only once call error handler Show error message also once - if abort reason not specified
1 parent ebfbb95 commit 0485ec9

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

modules/core.mjs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ settings = {
360360
/** @summary THttpServer read timeout in ms
361361
* @desc Configures timeout for requests to THttpServer
362362
* @default 0 */
363-
ServerTimeout: 5,
363+
ServerTimeout: 0,
364364
/** @summary Configure xhr.withCredentials = true when submitting http requests from JSROOT */
365365
WithCredentials: false,
366366
/** @summary Skip streamer infos from the GUI */
@@ -964,13 +964,21 @@ function findFunction(name) {
964964
/** @summary Method to create http request, without promise can be used only in browser environment
965965
* @private */
966966
function createHttpRequest(url, kind, user_accept_callback, user_reject_callback, use_promise, tmout) {
967+
function handle_error(xhr, message, code, abort_reason) {
968+
if (!xhr.did_abort) {
969+
xhr.did_abort = abort_reason || true;
970+
xhr.abort();
971+
}
972+
if (!xhr.did_error || abort_reason)
973+
console.warn(message);
974+
if (!xhr.did_error) {
975+
xhr.did_error = true;
976+
xhr.error_callback(Error(message), code);
977+
}
978+
}
967979
function configureXhr(xhr) {
968980
xhr.http_callback = isFunc(user_accept_callback) ? user_accept_callback.bind(xhr) : () => {};
969-
xhr.error_callback = isFunc(user_reject_callback) ? user_reject_callback.bind(xhr) : function(err) {
970-
if (err?.message)
971-
console.warn(err.message);
972-
this.http_callback(null);
973-
}.bind(xhr);
981+
xhr.error_callback = isFunc(user_reject_callback) ? user_reject_callback.bind(xhr) : function() { this.http_callback(null); };
974982

975983
if (!kind)
976984
kind = 'buf';
@@ -1006,11 +1014,8 @@ function createHttpRequest(url, kind, user_accept_callback, user_reject_callback
10061014

10071015
if (settings.HandleWrongHttpResponse && (method === 'GET') && isFunc(xhr.addEventListener)) {
10081016
xhr.addEventListener('progress', function(oEvent) {
1009-
if (oEvent.lengthComputable && this.expected_size && (oEvent.loaded > this.expected_size)) {
1010-
this.did_abort = true;
1011-
this.abort();
1012-
this.error_callback(Error(`Server sends more bytes ${oEvent.loaded} than expected ${this.expected_size}. Abort I/O operation`), 598);
1013-
}
1017+
if (oEvent.lengthComputable && this.expected_size && (oEvent.loaded > this.expected_size))
1018+
handle_error(this, `Server sends more bytes ${oEvent.loaded} than expected ${this.expected_size}. Abort I/O operation`, 598);
10141019
}.bind(xhr));
10151020
}
10161021

@@ -1020,11 +1025,8 @@ function createHttpRequest(url, kind, user_accept_callback, user_reject_callback
10201025

10211026
if ((this.readyState === 2) && this.expected_size) {
10221027
const len = parseInt(this.getResponseHeader('Content-Length'));
1023-
if (Number.isInteger(len) && (len > this.expected_size) && !settings.HandleWrongHttpResponse) {
1024-
this.did_abort = 'large';
1025-
this.abort();
1026-
return this.error_callback(Error(`Server response size ${len} larger than expected ${this.expected_size}. Abort I/O operation`), 599);
1027-
}
1028+
if (Number.isInteger(len) && (len > this.expected_size) && !settings.HandleWrongHttpResponse)
1029+
return handle_error(this, `Server response size ${len} larger than expected ${this.expected_size}. Abort I/O operation`, 599, 'large');
10281030
}
10291031

10301032
if (this.readyState !== 4)
@@ -1033,7 +1035,7 @@ function createHttpRequest(url, kind, user_accept_callback, user_reject_callback
10331035
if ((this.status !== 200) && (this.status !== 206) && !browser.qt6 &&
10341036
// in these special cases browsers not always set status
10351037
!((this.status === 0) && ((url.indexOf('file://') === 0) || (url.indexOf('blob:') === 0))))
1036-
return this.error_callback(Error(`Fail to load url ${url}`), this.status);
1038+
return handle_error(this, `Fail to load url ${url}`, this.status);
10371039

10381040
if (this.nodejs_checkzip && (this.getResponseHeader('content-encoding') === 'gzip')) {
10391041
// special handling of gzip JSON objects in Node.js
@@ -1080,11 +1082,7 @@ function createHttpRequest(url, kind, user_accept_callback, user_reject_callback
10801082

10811083
if (tmout && Number.isFinite(tmout)) {
10821084
xhr.timeout = tmout;
1083-
xhr.ontimeout = function() {
1084-
this.did_abort = true;
1085-
this.abort();
1086-
this.error_callback(Error(`Request ${url} timeout`));
1087-
};
1085+
xhr.ontimeout = function() { handle_error(this, `Request ${url} timeout set ${tmout} ms`, 600, 'timeout'); };
10881086
}
10891087

10901088
return xhr;

0 commit comments

Comments
 (0)