Commit 833a4ab8 authored by Michael Ott's avatar Michael Ott
Browse files

Throw exceptions instead of logging error messages in IPMIHost

parent ce1d3ba2
......@@ -20,6 +20,7 @@
#include <freeipmi/freeipmi.h>
#include <freeipmi/api/ipmi-api.h>
#include <freeipmi/sensor-read/ipmi-sensor-read.h>
#define RETRIES 2
......@@ -112,8 +113,6 @@ void IPMIHost::checkConnection() {
}
bool IPMIHost::getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record) {
bool ret = false;
try {
checkConnection();
} catch (const std::runtime_error& e) {
......@@ -122,10 +121,12 @@ bool IPMIHost::getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record) {
ipmi_sdr_ctx_t sdrCtx = ipmi_sdr_ctx_create();
if (!sdrCtx) {
return ret;
throw std::runtime_error("Error creating SDR context: " + std::string(strerror(errno)));
return false;
}
std::string errorMsg;
bool success = false;
int retries = RETRIES;
while (retries--) {
if (ipmi_sdr_cache_open(sdrCtx, _ipmiCtx, _cache.c_str()) < 0) {
......@@ -142,7 +143,7 @@ bool IPMIHost::getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record) {
}
} else {
if (retries == 0) {
LOG(error) << _hostName << ": ipmi_sdr_cache_open " << ipmi_sdr_ctx_errormsg(sdrCtx);
errorMsg = "Error opening SDR cache: " + std::string(ipmi_sdr_ctx_errormsg(sdrCtx));
}
increaseErrorCount();
disconnect();
......@@ -159,18 +160,18 @@ bool IPMIHost::getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record) {
if (ipmi_sdr_cache_search_record_id(sdrCtx, recordId) < 0) {
increaseErrorCount();
if (retries == 0) {
LOG(error) << _hostName << ": ipmi_sdr_cache_search_record_id() " << ipmi_sdr_ctx_errormsg(sdrCtx);
errorMsg = "Error searching SDR record: " + std::string(ipmi_sdr_ctx_errormsg(sdrCtx));
}
} else {
if ((recordLength = ipmi_sdr_cache_record_read(sdrCtx, recordBuf, IPMI_SDR_MAX_RECORD_LENGTH)) < 0) {
increaseErrorCount();
if (retries == 0) {
LOG(error) << _hostName << ": ipmi_sdr_cache_record_read " << ipmi_sdr_ctx_errormsg(sdrCtx);
errorMsg = "Error reading SDR record: " + std::string(ipmi_sdr_ctx_errormsg(sdrCtx));
}
} else {
_lastRead = getTimestamp();
record.insert(record.end(), &recordBuf[0], &recordBuf[recordLength]);
ret = true;
success = true;
}
}
ipmi_sdr_cache_close(sdrCtx);
......@@ -178,7 +179,11 @@ bool IPMIHost::getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record) {
}
ipmi_sdr_ctx_destroy(sdrCtx);
return ret;
if (!success) {
throw std::runtime_error(errorMsg);
}
return success;
}
uint64_t IPMIHost::sendRawCmd(const std::vector<uint8_t>& rawCmd,
......@@ -188,7 +193,7 @@ uint64_t IPMIHost::sendRawCmd(const std::vector<uint8_t>& rawCmd,
int i;
if (!IPMI_NET_FN_RQ_VALID(rawCmd[1])) {
LOG(error) << "IPMI: Invalid netfn value";
throw std::runtime_error("Error sending raw IPMI command: Invalid netfn value");
return 0;
}
......@@ -202,7 +207,8 @@ uint64_t IPMIHost::sendRawCmd(const std::vector<uint8_t>& rawCmd,
while (retries--) {
if ((len = ipmi_cmd_raw(_ipmiCtx, rawCmd[0], rawCmd[1], &rawCmd[2], rawCmd.size() - 2, buf, sizeof(buf))) < 0) {
if (retries == 0) {
LOG(error) << _hostName << ": ipmi_cmd_raw " << ipmi_ctx_errormsg(_ipmiCtx);
throw std::runtime_error("Error sending IPMI raw command: " + std::string(ipmi_ctx_errormsg(_ipmiCtx)));
return 0;
}
increaseErrorCount();
disconnect();
......@@ -259,7 +265,8 @@ double IPMIHost::readSensorRecord(std::vector<uint8_t>& record) {
if (ipmi_sensor_read(_sensorReadCtx, &record[0], record.size(), 0, &rawReading, &reading, &eventBitmask) < 0) {
if (retries == 0) {
LOG(error) << _hostName << ": ipmi_sensor_read " << ipmi_sensor_read_ctx_errormsg(_sensorReadCtx);
throw std::runtime_error("Error reading IPMI record: " + std::string(ipmi_sensor_read_ctx_errormsg(_sensorReadCtx)));
return 0;
}
increaseErrorCount();
disconnect();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment