Refactor: improved working with promises in testDatasource() method.

This commit is contained in:
Alexander Zobnin
2016-11-11 19:38:23 +03:00
parent 145bc80e78
commit 062d975319

View File

@@ -8,6 +8,7 @@ import DataProcessor from './DataProcessor';
import './zabbixAPI.service.js'; import './zabbixAPI.service.js';
import './zabbixCache.service.js'; import './zabbixCache.service.js';
import './queryProcessor.service.js'; import './queryProcessor.service.js';
import {ZabbixAPIError} from './zabbixAPICore.service.js';
class ZabbixAPIDatasource { class ZabbixAPIDatasource {
@@ -257,37 +258,33 @@ class ZabbixAPIDatasource {
* @return {object} Connection status and Zabbix API version * @return {object} Connection status and Zabbix API version
*/ */
testDatasource() { testDatasource() {
let zabbixVersion;
return this.zabbixAPI.getVersion() return this.zabbixAPI.getVersion()
.then(version => { .then(version => {
return this.zabbixAPI.login() zabbixVersion = version;
.then(auth => { return this.zabbixAPI.login();
if (auth) { })
.then(() => {
return { return {
status: "success", status: "success",
title: "Success", title: "Success",
message: "Zabbix API version: " + version message: "Zabbix API version: " + zabbixVersion
}; };
} else { })
return { .catch(error => {
status: "error", if (error instanceof ZabbixAPIError) {
title: "Invalid user name or password",
message: "Zabbix API version: " + version
};
}
}, error => {
return { return {
status: "error", status: "error",
title: error.message, title: error.message,
message: error.data message: error.data
}; };
}); } else {
}, error => {
console.log(error);
return { return {
status: "error", status: "error",
title: "Connection failed", title: "Connection failed",
message: "Could not connect to given url" message: "Could not connect to given url"
}; };
}
}); });
} }