Improved both api request() in zabbixAPI and zabbixAPIService.
Improved api request error handling.
This commit is contained in:
@@ -20,7 +20,7 @@ function (angular, _) {
|
||||
this.url = api_url;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.auth = null;
|
||||
this.auth = "";
|
||||
|
||||
this.requestOptions = {
|
||||
basicAuth: basicAuth,
|
||||
@@ -38,33 +38,29 @@ function (angular, _) {
|
||||
|
||||
p.request = function(method, params) {
|
||||
var self = this;
|
||||
if (this.auth) {
|
||||
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
|
||||
.then(function(result) {
|
||||
return result;
|
||||
},
|
||||
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
|
||||
.then(function(result) {
|
||||
return result;
|
||||
},
|
||||
|
||||
// Handle errors
|
||||
function(error) {
|
||||
if (error.message === "Session terminated, re-login, please.") {
|
||||
throw 'expired';
|
||||
return self.login().then(function(auth) {
|
||||
self.auth = auth;
|
||||
return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth);
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
|
||||
// Login first
|
||||
//throw 'unauthenticated';
|
||||
return self.loginOnce().then(function(auth) {
|
||||
self.auth = auth;
|
||||
return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth);
|
||||
// Handle errors
|
||||
function(error) {
|
||||
if (isAuthError(error.data)) {
|
||||
return self.loginOnce().then(function() {
|
||||
return self.request(method, params);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function isAuthError(message) {
|
||||
return (
|
||||
message === "Session terminated, re-login, please." ||
|
||||
message === "Not authorised." ||
|
||||
message === "Not authorized."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When API unauthenticated or auth token expired each request produce login()
|
||||
* call. But auth token is common to all requests. This function wraps login() method
|
||||
@@ -78,6 +74,7 @@ function (angular, _) {
|
||||
self.loginPromise = deferred.promise;
|
||||
self.login().then(function(auth) {
|
||||
self.loginPromise = null;
|
||||
self.auth = auth;
|
||||
deferred.resolve(auth);
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -17,6 +17,7 @@ function (angular) {
|
||||
* @return {object} response.result
|
||||
*/
|
||||
this.request = function(api_url, method, params, options, auth) {
|
||||
var deferred = $q.defer();
|
||||
var requestData = {
|
||||
jsonrpc: '2.0',
|
||||
method: method,
|
||||
@@ -24,8 +25,12 @@ function (angular) {
|
||||
id: 1
|
||||
};
|
||||
|
||||
// Set auth parameter only if it needed
|
||||
if (auth) {
|
||||
if (auth === "") {
|
||||
// Reject immediately if not authenticated
|
||||
deferred.reject({data: "Not authorised."});
|
||||
return deferred.promise;
|
||||
} else if (auth) {
|
||||
// Set auth parameter only if it needed
|
||||
requestData.auth = auth;
|
||||
}
|
||||
|
||||
@@ -46,19 +51,20 @@ function (angular) {
|
||||
requestOptions.headers.Authorization = options.basicAuth;
|
||||
}
|
||||
|
||||
return backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
||||
backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
||||
// General connection issues
|
||||
if (!response.data) {
|
||||
return [];
|
||||
deferred.reject(response);
|
||||
}
|
||||
|
||||
// Handle Zabbix API errors
|
||||
else if (response.data.error) {
|
||||
throw new ZabbixException(response.data.error);
|
||||
deferred.reject(response.data.error);
|
||||
}
|
||||
|
||||
return response.data.result;
|
||||
deferred.resolve(response.data.result);
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user