Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ba7a1ea

Browse files
tipycalFlowNarretz
authored andcommitted
Adding separate handlers for xhr
Adding param xhrStatus to response and separate handlers for onerror, onabort, ontimeout
1 parent 43ed2b7 commit ba7a1ea

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/ng/httpBackend.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
6464
var jsonpDone = jsonpReq(url, callbackPath, function(status, text) {
6565
// jsonpReq only ever sets status to 200 (OK), 404 (ERROR) or -1 (WAITING)
6666
var response = (status === 200) && callbacks.getResponse(callbackPath);
67-
completeRequest(callback, status, response, '', text);
67+
completeRequest(callback, status, response, '', text, '');
6868
callbacks.removeCallback(callbackPath);
6969
});
7070
} else {
@@ -99,18 +99,27 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
9999
status,
100100
response,
101101
xhr.getAllResponseHeaders(),
102-
statusText);
102+
statusText,
103+
'Request Completed');
103104
};
104105

105106
var requestError = function() {
106107
// The response is always empty
107108
// See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
108-
completeRequest(callback, -1, null, null, '');
109+
completeRequest(callback, -1, null, null, '', 'Request Error');
110+
};
111+
112+
var requestAborted = function() {
113+
completeRequest(callback, -1, null, null, '', 'Request Aborted');
114+
};
115+
116+
var requestTimedOut = function() {
117+
completeRequest(callback, -1, null, null, '', 'Request Timed Out');
109118
};
110119

111120
xhr.onerror = requestError;
112-
xhr.onabort = requestError;
113-
xhr.ontimeout = requestError;
121+
xhr.onabort = requestAborted;
122+
xhr.ontimeout = requestTimedOut;
114123

115124
forEach(eventHandlers, function(value, key) {
116125
xhr.addEventListener(key, value);
@@ -160,14 +169,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
160169
}
161170
}
162171

163-
function completeRequest(callback, status, response, headersString, statusText) {
172+
function completeRequest(callback, status, response, headersString, statusText, xhrStatus) {
164173
// cancel timeout and subsequent timeout promise resolution
165174
if (isDefined(timeoutId)) {
166175
$browserDefer.cancel(timeoutId);
167176
}
168177
jsonpDone = xhr = null;
169178

170-
callback(status, response, headersString, statusText);
179+
callback(status, response, headersString, statusText, xhrStatus);
171180
}
172181
};
173182

0 commit comments

Comments
 (0)