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.url = api_url;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.auth = null;
|
this.auth = "";
|
||||||
|
|
||||||
this.requestOptions = {
|
this.requestOptions = {
|
||||||
basicAuth: basicAuth,
|
basicAuth: basicAuth,
|
||||||
@@ -38,33 +38,29 @@ function (angular, _) {
|
|||||||
|
|
||||||
p.request = function(method, params) {
|
p.request = function(method, params) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if (this.auth) {
|
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
|
||||||
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
|
.then(function(result) {
|
||||||
.then(function(result) {
|
return result;
|
||||||
return result;
|
},
|
||||||
},
|
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
function(error) {
|
function(error) {
|
||||||
if (error.message === "Session terminated, re-login, please.") {
|
if (isAuthError(error.data)) {
|
||||||
throw 'expired';
|
return self.loginOnce().then(function() {
|
||||||
return self.login().then(function(auth) {
|
return self.request(method, params);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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()
|
* 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
|
* 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.loginPromise = deferred.promise;
|
||||||
self.login().then(function(auth) {
|
self.login().then(function(auth) {
|
||||||
self.loginPromise = null;
|
self.loginPromise = null;
|
||||||
|
self.auth = auth;
|
||||||
deferred.resolve(auth);
|
deferred.resolve(auth);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ function (angular) {
|
|||||||
* @return {object} response.result
|
* @return {object} response.result
|
||||||
*/
|
*/
|
||||||
this.request = function(api_url, method, params, options, auth) {
|
this.request = function(api_url, method, params, options, auth) {
|
||||||
|
var deferred = $q.defer();
|
||||||
var requestData = {
|
var requestData = {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: method,
|
method: method,
|
||||||
@@ -24,8 +25,12 @@ function (angular) {
|
|||||||
id: 1
|
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;
|
requestData.auth = auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,19 +51,20 @@ function (angular) {
|
|||||||
requestOptions.headers.Authorization = options.basicAuth;
|
requestOptions.headers.Authorization = options.basicAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
return backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
||||||
// General connection issues
|
// General connection issues
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
return [];
|
deferred.reject(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Zabbix API errors
|
// Handle Zabbix API errors
|
||||||
else if (response.data.error) {
|
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