Browse Source

Improve error reporting when network request fails

master
Igor Zhukov 9 years ago
parent
commit
ecb4871723
  1. 23
      app/js/lib/mtproto.js
  2. 2
      app/js/lib/mtproto_wrapper.js
  3. 1
      app/partials/desktop/error_modal.html

23
app/js/lib/mtproto.js

@ -208,18 +208,20 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
var requestData = xhrSendBuffer ? resultBuffer : resultArray, var requestData = xhrSendBuffer ? resultBuffer : resultArray,
requestPromise; requestPromise;
var url = MtpDcConfigurator.chooseServer(dcID);
var baseError = {code: 406, type: 'NETWORK_BAD_RESPONSE', url: url};
try { try {
requestPromise = $http.post(MtpDcConfigurator.chooseServer(dcID), requestData, { requestPromise = $http.post(url, requestData, {
responseType: 'arraybuffer', responseType: 'arraybuffer',
transformRequest: null transformRequest: null
}); });
} catch (e) { } catch (e) {
requestPromise = $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE', originalError: e}); requestPromise = $q.reject(angular.extend(baseError, {originalError: e}));
} }
return requestPromise.then( return requestPromise.then(
function (result) { function (result) {
if (!result.data || !result.data.byteLength) { if (!result.data || !result.data.byteLength) {
return $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE'}); return $q.reject(baseError);
} }
try { try {
@ -230,14 +232,14 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
var msg_len = deserializer.fetchInt('msg_len'); var msg_len = deserializer.fetchInt('msg_len');
} catch (e) { } catch (e) {
return $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE', originalError: e}); return $q.reject(angular.extend(baseError, {originalError: e}));
} }
return deserializer; return deserializer;
}, },
function (error) { function (error) {
if (!error.message && !error.type) { if (!error.message && !error.type) {
error = {code: 406, type: 'NETWORK_BAD_REQUEST', originalError: error}; error = angular.extend(baseError, {originalError: error});
} }
return $q.reject(error); return $q.reject(error);
} }
@ -286,7 +288,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
deferred.reject(error); deferred.reject(error);
}); });
}, function (error) { }, function (error) {
console.log(dT(), 'req_pq error', error.message); console.error(dT(), 'req_pq error', error.message);
deferred.reject(error); deferred.reject(error);
}); });
@ -1201,19 +1203,22 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
var requestData = xhrSendBuffer ? request.getBuffer() : request.getArray(); var requestData = xhrSendBuffer ? request.getBuffer() : request.getArray();
var requestPromise; var requestPromise;
var url = MtpDcConfigurator.chooseServer(self.dcID, self.upload);
var baseError = {code: 406, type: 'NETWORK_BAD_RESPONSE', url: url};
try { try {
options = angular.extend(options || {}, { options = angular.extend(options || {}, {
responseType: 'arraybuffer', responseType: 'arraybuffer',
transformRequest: null transformRequest: null
}); });
requestPromise = $http.post(MtpDcConfigurator.chooseServer(self.dcID, self.upload), requestData, options); requestPromise = $http.post(url, requestData, options);
} catch (e) { } catch (e) {
requestPromise = $q.reject(e); requestPromise = $q.reject(e);
} }
return requestPromise.then( return requestPromise.then(
function (result) { function (result) {
if (!result.data || !result.data.byteLength) { if (!result.data || !result.data.byteLength) {
return $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE'}); return $q.reject(baseError);
} }
return result; return result;
}, },
@ -1228,7 +1233,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
}); });
} }
if (!error.message && !error.type) { if (!error.message && !error.type) {
error = {code: 406, type: 'NETWORK_BAD_REQUEST'}; error = angular.extend(baseError, {type: 'NETWORK_BAD_REQUEST', originalError: error});
} }
return $q.reject(error); return $q.reject(error);
} }

2
app/js/lib/mtproto_wrapper.js

@ -216,7 +216,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
networker.wrapApiCall(method, params, options).then(function (result) { networker.wrapApiCall(method, params, options).then(function (result) {
deferred.resolve(result); deferred.resolve(result);
}, rejectPromise); }, rejectPromise);
}); }, rejectPromise);
} }
} }
else if (!options.rawError && error.code == 420) { else if (!options.rawError && error.code == 420) {

1
app/partials/desktop/error_modal.html

@ -63,6 +63,7 @@
<div ng-if="error" class="error_modal_details" ng-switch="error.detailsShown"> <div ng-if="error" class="error_modal_details" ng-switch="error.detailsShown">
<textarea ng-switch-when="true" rows="3" onclick="this.select()">Method: {{error.input || 'N/A'}} <textarea ng-switch-when="true" rows="3" onclick="this.select()">Method: {{error.input || 'N/A'}}
Url: {{error.url || 'N/A'}}
Result: {{error.originalError ? error.originalError : (error.stack ? (error.name || '') + ' ' + (error.description || error.message) : error)}} Result: {{error.originalError ? error.originalError : (error.stack ? (error.name || '') + ' ' + (error.description || error.message) : error)}}
Stack: {{error.originalError.stack || error.stack}}</textarea> Stack: {{error.originalError.stack || error.stack}}</textarea>
<div ng-switch-default> <div ng-switch-default>

Loading…
Cancel
Save