Skip to content

Commit

Permalink
添加异常捕获
Browse files Browse the repository at this point in the history
  • Loading branch information
wendux committed Jan 23, 2019
1 parent 1976fbc commit 95d79f6
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 242 deletions.
70 changes: 37 additions & 33 deletions dist/fly.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,40 +411,44 @@ var Fly = function () {
}

engine.onload = function () {
// The xhr of IE9 has not response field
var response = engine.response || engine.responseText;
if (response && options.parseJson && (engine.getResponseHeader(contentType) || "").indexOf("json") !== -1
// Some third engine implementation may transform the response text to json object automatically,
// so we should test the type of response before transforming it
&& !utils.isObject(response)) {
response = JSON.parse(response);
}
try {
// The xhr of IE9 has not response field
var response = engine.response || engine.responseText;
if (response && options.parseJson && (engine.getResponseHeader(contentType) || "").indexOf("json") !== -1
// Some third engine implementation may transform the response text to json object automatically,
// so we should test the type of response before transforming it
&& !utils.isObject(response)) {
response = JSON.parse(response);
}

var headers = engine.responseHeaders;
// In browser
if (!headers) {
headers = {};
var items = (engine.getAllResponseHeaders() || "").split("\r\n");
items.pop();
items.forEach(function (e) {
if (!e) return;
var key = e.split(":")[0];
headers[key] = engine.getResponseHeader(key);
});
}
var status = engine.status;
var statusText = engine.statusText;
var data = { data: response, headers: headers, status: status, statusText: statusText };
// The _response filed of engine is set in adapter which be called in engine-wrapper.js
utils.merge(data, engine._response);
if (status >= 200 && status < 300 || status === 304) {
data.engine = engine;
data.request = options;
onresult(responseInterceptor.handler, data, 0);
} else {
var e = new Err(statusText, status);
e.response = data;
onerror(e);
var headers = engine.responseHeaders;
// In browser
if (!headers) {
headers = {};
var items = (engine.getAllResponseHeaders() || "").split("\r\n");
items.pop();
items.forEach(function (e) {
if (!e) return;
var key = e.split(":")[0];
headers[key] = engine.getResponseHeader(key);
});
}
var status = engine.status;
var statusText = engine.statusText;
var data = {data: response, headers: headers, status: status, statusText: statusText};
// The _response filed of engine is set in adapter which be called in engine-wrapper.js
utils.merge(data, engine._response);
if (status >= 200 && status < 300 || status === 304) {
data.engine = engine;
data.request = options;
onresult(responseInterceptor.handler, data, 0);
} else {
var e = new Err(statusText, status);
e.response = data;
onerror(e);
}
} catch (e) {
onerror(new Err(e.msg, engine.status));
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/fly.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 95d79f6

Please sign in to comment.