Skip to content

Commit

Permalink
Improve exception messages when configuration keys are missing or inv…
Browse files Browse the repository at this point in the history
…alid.

Include a message in the exception, and reuse code to make them consistent.

Whitespace cleanup.

(cherry picked from commit 8589bd9)
  • Loading branch information
qris committed Jan 15, 2018
1 parent ab86c81 commit feeee65
Showing 1 changed file with 39 additions and 59 deletions.
98 changes: 39 additions & 59 deletions lib/common/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,11 @@ const std::string &Configuration::GetKeyValue(const std::string& rKeyName) const

if(i == mKeys.end())
{
BOX_LOG_CATEGORY(Log::ERROR, ConfigurationVerify::VERIFY_ERROR,
THROW_EXCEPTION_MESSAGE(CommonException, ConfigNoKey,
"Missing configuration key: " << rKeyName);
THROW_EXCEPTION(CommonException, ConfigNoKey)
}
else
{
return i->second;
}

return i->second;
}


Expand All @@ -451,22 +448,17 @@ const std::string &Configuration::GetKeyValue(const std::string& rKeyName) const
// --------------------------------------------------------------------------
int Configuration::GetKeyValueInt(const std::string& rKeyName) const
{
std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName));

if(i == mKeys.end())
{
THROW_EXCEPTION(CommonException, ConfigNoKey)
}
else
std::string value_str = GetKeyValue(rKeyName);
long value = ::strtol(value_str.c_str(), NULL, 0 /* C style handling */);

if(value == LONG_MAX || value == LONG_MIN)
{
long value = ::strtol((i->second).c_str(), NULL,
0 /* C style handling */);
if(value == LONG_MAX || value == LONG_MIN)
{
THROW_EXCEPTION(CommonException, ConfigBadIntValue)
}
return (int)value;
THROW_EXCEPTION_MESSAGE(CommonException, ConfigBadIntValue,
"Invalid integer value for configuration key: " <<
rKeyName << ": '" << value_str << "'");
}

return (int)value;
}


Expand All @@ -480,23 +472,18 @@ int Configuration::GetKeyValueInt(const std::string& rKeyName) const
// --------------------------------------------------------------------------
uint32_t Configuration::GetKeyValueUint32(const std::string& rKeyName) const
{
std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName));

if(i == mKeys.end())
{
THROW_EXCEPTION(CommonException, ConfigNoKey)
}
else
std::string value_str = GetKeyValue(rKeyName);
errno = 0;
long value = ::strtoul(value_str.c_str(), NULL, 0 /* C style handling */);

if(errno != 0)
{
errno = 0;
long value = ::strtoul((i->second).c_str(), NULL,
0 /* C style handling */);
if(errno != 0)
{
THROW_EXCEPTION(CommonException, ConfigBadIntValue)
}
return (int)value;
THROW_EXCEPTION_MESSAGE(CommonException, ConfigBadIntValue,
"Invalid integer value for configuration key: " <<
rKeyName << ": '" << value_str << "'");
}

return (int)value;
}


Expand All @@ -510,33 +497,24 @@ uint32_t Configuration::GetKeyValueUint32(const std::string& rKeyName) const
// --------------------------------------------------------------------------
bool Configuration::GetKeyValueBool(const std::string& rKeyName) const
{
std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName));

if(i == mKeys.end())
{
THROW_EXCEPTION(CommonException, ConfigNoKey)
}
else
std::string value_str = GetKeyValue(rKeyName);
bool value = false;

// Anything this is called for should have been verified as having a correct
// string in the verification section. However, this does default to false
// if it isn't in the string table.

for(int l = 0; sValueBooleanStrings[l] != 0; ++l)
{
bool value = false;

// Anything this is called for should have been verified as having a correct
// string in the verification section. However, this does default to false
// if it isn't in the string table.

for(int l = 0; sValueBooleanStrings[l] != 0; ++l)
if(::strcasecmp(value_str.c_str(), sValueBooleanStrings[l]) == 0)
{
if(::strcasecmp((i->second).c_str(), sValueBooleanStrings[l]) == 0)
{
// Found.
value = sValueBooleanValue[l];
break;
}
// Found.
value = sValueBooleanValue[l];
break;
}

return value;
}

return value;
}


Expand Down Expand Up @@ -617,7 +595,8 @@ const Configuration &Configuration::GetSubConfiguration(const std::string&
}
}

THROW_EXCEPTION(CommonException, ConfigNoSubConfig)
THROW_EXCEPTION_MESSAGE(CommonException, ConfigNoSubConfig,
"Missing sub-configuration section: " << rSubName);
}


Expand Down Expand Up @@ -647,7 +626,8 @@ Configuration &Configuration::GetSubConfigurationEditable(const std::string&
}
}

THROW_EXCEPTION(CommonException, ConfigNoSubConfig)
THROW_EXCEPTION_MESSAGE(CommonException, ConfigNoSubConfig,
"Missing sub-configuration section: " << rSubName);
}


Expand Down

0 comments on commit feeee65

Please sign in to comment.