This repository was archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handling.js
More file actions
142 lines (127 loc) · 4.32 KB
/
error_handling.js
File metadata and controls
142 lines (127 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*globals Ext, SWorks, console */
/*jslint glovar: true, undef: true, nomen: true */
Ext.namespace('SWorks');
SWorks.ErrorHandling = {
serverError: function(response, result) {
console.trace();
if(typeof response !== 'object' || response.serverErrorDispatched !== true) {
var msg = (typeof result === 'object' &&
typeof result.message == 'string' ? result.message : null ) ||
"The server had a problem. Please report the issue.";
Ext.MessageBox.alert('Error', msg);
if (typeof response == 'object') {
response.serverErrorDispatched = true;
}
}
},
clientError: function(message) {
message = typeof message == 'string' ? message :
"There was an internal error. Please report the issue and reload the application.";
console.trace();
Ext.MessageBox.alert('Application Error', message);
},
onAjaxRequestComplete: function(conn, resp, opts) {
this.addData([true, resp, opts]);
var result = null;
try {
result = Ext.decode(resp.responseText);
if (typeof result === 'object' && result.success === false) {
this.serverError(resp, result);
this.logError(resp, opts);
}
} catch(e) {
console.error("Failed to parse response ("+resp.tId+"): " + e.message);
}
},
onAjaxRequestException: function(conn, resp, opts) {
this.addData([false, resp, opts]);
//resp.status == HTTP status code
if(resp.status === 0) {
Ext.MessageBox.alert('Server Not Found',
"Failed to connect to the server. Please report the issue.");
} else {
if (resp.status == -1) {
var m = opts.method;
if ((!m || m === '' || m == 'GET' || opts.forceRetryRequest === true) &&
(typeof opts.retryAttempts != 'number' || opts.retryAttempts > 0)) {
// if retryAttempts == null, set = 1 and try which gives 1 more
// retry after this, so retryAttempts == null gets 2 retrys
opts.retryAttempts = (typeof opts.retryAttempts == 'number') ?
opts.retryAttempts - 1 : 1;
Ext.Ajax.request(opts);
return false; // halt the error propogation
} else {
Ext.MessageBox.alert('Error',
"The server was too slow. The request may or may not have completed.");
}
} else {
this.serverError(resp);
this.logError(resp, opts);
}
}
},
saveSerializedFormData: function(conn, options) {
if(options.form) {
options.serializedForm = Ext.lib.Ajax.serializeForm(options.form);
}
},
logError: function(response, options) {
if(options.loggingRequest !== true) {
var data = {
serverResponse: response,
clientRequest: {
url: options.url,
parameters: options.params,
formData: options.serializedForm
}
};
if (options.scope &&
options.scope.options &&
options.scope.options.dataSentRecord) {
data.clientRequest.dataRecord = options.scope.options.dataSentRecord;
}
SWorks.Logging.error(data);
}
},
addData: function(list) {
this.dataList.push(list);
if(this.dataList.length > 100) {
this.dataList.pop();
}
},
onExtReady: function() {
this.dataList = this.dataList || [];
Ext.Ajax.on('beforerequest', this.saveSerializedFormData, this);
Ext.Ajax.on('requestcomplete', this.onAjaxRequestComplete, this);
Ext.Ajax.on('requestexception', this.onAjaxRequestException, this);
}
};
Ext.onReady(SWorks.ErrorHandling.onExtReady, SWorks.ErrorHandling);
Ext.override(Ext.data.Connection, {
handleJsonResponse: function(options, success, response) {
var result = null;
if(success) {
try {
result = Ext.decode(response.responseText);
} catch(e) {}
if (result && typeof result === 'object') {
if(options.jsonCallback &&
typeof options.jsonCallback.callback == 'function') {
options.jsonCallback.callback.call(
options.jsonCallback.scope,
result, options, response);
}
} else {
SWorks.ErrorHandling.serverError(response);
}
}
},
jsonRequest: function(opts) {
opts.jsonCallback = {
callback: opts.callback,
scope: opts.scope
};
opts.callback = this.handleJsonResponse;
this.request(opts);
}
});