@@ -41,20 +41,27 @@ function decodeJSONResponse(jsonString) {
41
41
//
42
42
// - onSuccessCallback (function(object)): A fn executed when a response is received
43
43
// from the server. The fn should take the returned object as its only argument
44
- function doPost ( requestType , requestData , onSuccessCallback ) {
44
+ function doPost ( requestType , requestData , onSuccessCallback , timeout , onErrorCallback ) {
45
45
const http = new XMLHttpRequest ( ) ;
46
46
const url = 'installer_data' ; // in python, is stored in 'self.path' on the handler class
47
47
48
+ function errorHandler ( msg ) {
49
+ const message = `[${ requestType } ] POST Error - "${ msg } " - You must have the install loader window/console open - it is required for the installation.` ;
50
+ console . log ( message ) ;
51
+ POSTNotificationErrorCallback ( message ) ;
52
+ if ( typeof onErrorCallback !== 'undefined' ) {
53
+ onErrorCallback ( msg ) ;
54
+ }
55
+ }
56
+
48
57
// in python, is retrieved by calling 'self.rfile.read(content_length)',
49
58
// where content_length is in the header (see python code)
50
59
http . open ( 'POST' , url , true ) ;
51
60
52
61
// Send the proper header information along with the request
53
62
http . setRequestHeader ( 'Content-type' , 'application/x-www-form-urlencoded' ) ;
54
63
55
- // Call a function when the state changes.
56
- // TODO: add timeout here to notify user if server has crashed or stopped working
57
- http . onreadystatechange = function onReadyStateChange ( ) {
64
+ http . onload = ( ) => {
58
65
if ( http . readyState === 4 ) {
59
66
if ( http . status === 200 ) {
60
67
const [ responseType , responseDataObject ] = decodeJSONResponse ( http . responseText ) ;
@@ -67,18 +74,26 @@ function doPost(requestType, requestData, onSuccessCallback) {
67
74
onSuccessCallback ( responseDataObject ) ;
68
75
}
69
76
} else {
70
- const errorCodeString = http . status === 0 ? '' : `[${ http . status } ]` ;
71
- const message = `POST Error ${ errorCodeString } on [${ requestType } ] - Please check the install loader window/console is open - it is required for the installation.` ;
72
- console . log ( message ) ;
73
- POSTNotificationErrorCallback ( message ) ;
77
+ errorHandler ( `Unexpected HTTP Status [${ http . status } ]` ) ;
74
78
}
75
79
}
76
80
} ;
77
81
78
- // Use a timeout of 8 seconds. After this POSTNotificationErrorCallback() will be called
79
- if ( requestType !== 'showFileChooser' ) {
80
- http . timeout = 8000 ;
81
- }
82
+ http . onabort = ( ) => {
83
+ errorHandler ( 'POST Aborted' ) ;
84
+ } ;
85
+
86
+ http . onerror = function ( ) {
87
+ errorHandler ( 'POST Error' ) ;
88
+ } ;
89
+
90
+ http . ontimeout = function ( e ) {
91
+ errorHandler ( `POST Timeout (${ http . timeout / 1000 } s)` ) ;
92
+ } ;
93
+
94
+ // Use a timeout of 8 seconds by default.
95
+ // After this errorHandler()/POSTNotificationErrorCallback() will be called
96
+ http . timeout = timeout !== 'undefined' ? timeout : 8000 ;
82
97
83
98
http . send ( makeJSONRequest ( requestType , requestData ) ) ;
84
99
}
0 commit comments