From ecb4871723cf086f7229bb7b3ab27820b95458ac Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 14 Jan 2016 22:32:42 +0300 Subject: [PATCH] Improve error reporting when network request fails --- app/js/lib/mtproto.js | 23 ++++++++++++++--------- app/js/lib/mtproto_wrapper.js | 2 +- app/partials/desktop/error_modal.html | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/js/lib/mtproto.js b/app/js/lib/mtproto.js index 89d5e890..9e68a995 100644 --- a/app/js/lib/mtproto.js +++ b/app/js/lib/mtproto.js @@ -208,18 +208,20 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) var requestData = xhrSendBuffer ? resultBuffer : resultArray, requestPromise; + var url = MtpDcConfigurator.chooseServer(dcID); + var baseError = {code: 406, type: 'NETWORK_BAD_RESPONSE', url: url}; try { - requestPromise = $http.post(MtpDcConfigurator.chooseServer(dcID), requestData, { + requestPromise = $http.post(url, requestData, { responseType: 'arraybuffer', transformRequest: null }); } catch (e) { - requestPromise = $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE', originalError: e}); + requestPromise = $q.reject(angular.extend(baseError, {originalError: e})); } return requestPromise.then( function (result) { if (!result.data || !result.data.byteLength) { - return $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE'}); + return $q.reject(baseError); } try { @@ -230,14 +232,14 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) var msg_len = deserializer.fetchInt('msg_len'); } catch (e) { - return $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE', originalError: e}); + return $q.reject(angular.extend(baseError, {originalError: e})); } return deserializer; }, function (error) { if (!error.message && !error.type) { - error = {code: 406, type: 'NETWORK_BAD_REQUEST', originalError: error}; + error = angular.extend(baseError, {originalError: error}); } return $q.reject(error); } @@ -286,7 +288,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) deferred.reject(error); }); }, function (error) { - console.log(dT(), 'req_pq error', error.message); + console.error(dT(), 'req_pq error', error.message); deferred.reject(error); }); @@ -1201,19 +1203,22 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) var requestData = xhrSendBuffer ? request.getBuffer() : request.getArray(); var requestPromise; + var url = MtpDcConfigurator.chooseServer(self.dcID, self.upload); + var baseError = {code: 406, type: 'NETWORK_BAD_RESPONSE', url: url}; + try { options = angular.extend(options || {}, { responseType: 'arraybuffer', transformRequest: null }); - requestPromise = $http.post(MtpDcConfigurator.chooseServer(self.dcID, self.upload), requestData, options); + requestPromise = $http.post(url, requestData, options); } catch (e) { requestPromise = $q.reject(e); } return requestPromise.then( function (result) { if (!result.data || !result.data.byteLength) { - return $q.reject({code: 406, type: 'NETWORK_BAD_RESPONSE'}); + return $q.reject(baseError); } return result; }, @@ -1228,7 +1233,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) }); } 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); } diff --git a/app/js/lib/mtproto_wrapper.js b/app/js/lib/mtproto_wrapper.js index 5e8847e1..103947af 100644 --- a/app/js/lib/mtproto_wrapper.js +++ b/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) { deferred.resolve(result); }, rejectPromise); - }); + }, rejectPromise); } } else if (!options.rawError && error.code == 420) { diff --git a/app/partials/desktop/error_modal.html b/app/partials/desktop/error_modal.html index 2c58dc14..3b7d7350 100644 --- a/app/partials/desktop/error_modal.html +++ b/app/partials/desktop/error_modal.html @@ -63,6 +63,7 @@