Improve auth error handle - more clear.

This commit is contained in:
Alexander Zobnin
2015-06-04 21:11:22 +03:00
parent 04ccba8501
commit 4dbbb496a8

View File

@@ -112,7 +112,7 @@ function (angular, _, kbn) {
/** /**
* Perform time series query from Zabbix API * Perform history query from Zabbix API
* *
* @param {Array} items Array of Zabbix item objects * @param {Array} items Array of Zabbix item objects
* @param {Number} start Time in seconds * @param {Number} start Time in seconds
@@ -192,7 +192,13 @@ function (angular, _, kbn) {
}; };
// Request data from Zabbix API /**
* Request data from Zabbix API
*
* @param {string} method Zabbix API method name
* @param {object} params method params
* @return {object} result
*/
ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(method, params) { ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(method, params) {
var options = { var options = {
method: 'POST', method: 'POST',
@@ -209,36 +215,20 @@ function (angular, _, kbn) {
} }
}; };
var performedQuery;
var self = this; var self = this;
return backendSrv.datasourceRequest(options).then(function (response) {
// Check authorization first
if (!this.auth) {
performedQuery = this.performZabbixAPILogin().then(function (response) {
self.auth = response;
options.data.auth = response;
return backendSrv.datasourceRequest(options);
});
} else {
performedQuery = backendSrv.datasourceRequest(options);
}
// Handle response
return performedQuery.then(function (response) {
if (!response.data) { if (!response.data) {
return []; return [];
} }
else if (response.data.error) { else if (response.data.error) {
// Handle Zabbix API errors // Handle Zabbix API errors
if (response.data.error.data == "Session terminated, re-login, please.") { if (response.data.error.data == "Session terminated, re-login, please." ||
// Handle "Session terminated, re-login, please." error response.data.error.data == 'Not authorised.') {
// Handle auth errors
return self.performZabbixAPILogin().then(function (response) { return self.performZabbixAPILogin().then(function (response) {
self.auth = response; self.auth = response;
options.data.auth = response; return self.performZabbixAPIRequest(method, params);
return backendSrv.datasourceRequest(options);
}).then(function (response) {
return response.data.result;
}); });
} }
} }