Skip to content

Commit

Permalink
Fix 'throw' messages throughout the code (#46)
Browse files Browse the repository at this point in the history
* Fix 'throw' messages throughout the code

Signed-off-by: lbgracioso <[email protected]>

* Fix clang-format bug on os.cpp

Signed-off-by: lbgracioso <[email protected]>

---------

Signed-off-by: lbgracioso <[email protected]>
  • Loading branch information
lbgracioso committed Feb 28, 2024
1 parent c8514ae commit 2a2e501
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ const std::string& Cluster::getDomainName() const { return m_domainName; }
void Cluster::setDomainName(const std::string& domainName)
{
if (domainName.size() > 255)
throw;
throw std::length_error("Domain name exceeds the maximum allowed "
"length of 255 characters.");

#if __cpp_lib_starts_ends_with >= 201711L
if (domainName.starts_with('-') or domainName.ends_with('-'))
Expand All @@ -94,11 +95,14 @@ void Cluster::setDomainName(const std::string& domainName)

/* Check if string has only digits */
if (std::regex_match(domainName, std::regex("^[0-9]+$")))
throw;
throw std::invalid_argument(
"Domain name should not consist solely of numeric digits.");

/* Check if it's not only alphanumerics and - */
if (!(std::regex_match(domainName, std::regex("^[A-Za-z0-9-.]+$"))))
throw;
throw std::invalid_argument(
"Domain name contains invalid characters. Only alphanumeric "
"characters and hyphens are allowed.");

m_domainName = domainName;

Expand Down
12 changes: 8 additions & 4 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ uint16_t Network::getVLAN() const { return m_vlan; }
void Network::setVLAN(uint16_t vlan)
{
if (vlan >= 4096)
throw;
throw std::out_of_range("VLAN value must be less than 4096.");
m_vlan = vlan;
}

Expand All @@ -286,7 +286,8 @@ const std::string& Network::getDomainName() const { return m_domainName; }
void Network::setDomainName(const std::string& domainName)
{
if (domainName.size() > 255)
throw;
throw std::length_error("Domain name exceeds the maximum allowed "
"length of 255 characters.");

#if __cpp_lib_starts_ends_with >= 201711L
if (domainName.starts_with('-') or domainName.ends_with('-'))
Expand All @@ -298,11 +299,14 @@ void Network::setDomainName(const std::string& domainName)

/* Check if string has only digits */
if (std::regex_match(domainName, std::regex("^[0-9]+$")))
throw;
throw std::invalid_argument(
"Domain name should not consist solely of numeric digits.");

/* Check if it's not only alphanumerics and - */
if (!(std::regex_match(domainName, std::regex("^[A-Za-z0-9-.]+$"))))
throw;
throw std::invalid_argument(
"Domain name contains invalid characters. Only alphanumeric "
"characters and hyphens are allowed.");

m_domainName = domainName;
}
Expand Down
12 changes: 8 additions & 4 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ OS::OS()

if (!file.is_open()) {
perror(("Error while opening file " + filename).c_str());
throw; /* Error opening file */
throw std::runtime_error(
fmt::format("Error while opening file: {}", filename));
}

/* Fetches OS information from /etc/os-release. The file is writen in a
Expand Down Expand Up @@ -83,7 +84,8 @@ OS::OS()

if (file.bad()) {
perror(("Error while reading file " + filename).c_str());
throw; /* Error while reading file */
throw std::runtime_error(
fmt::format("Error while reading file: {}", filename));
}
}
}
Expand Down Expand Up @@ -177,7 +179,8 @@ unsigned int OS::getMajorVersion() const { return m_majorVersion; }
void OS::setMajorVersion(unsigned int majorVersion)
{
if (majorVersion < 8)
throw; /* Unsupported release */
throw std::runtime_error(
"Unsupported release: Major version must be 8 or greater.");

m_majorVersion = majorVersion;
}
Expand All @@ -187,7 +190,8 @@ unsigned int OS::getMinorVersion() const { return m_minorVersion; }
void OS::setMinorVersion(unsigned int minorVersion)
{
if (minorVersion < 1)
throw; /* Unsupported minor release */
throw std::runtime_error(
"Unsupported release: Minor version must be 1 or greater.");

m_minorVersion = minorVersion;
}
Expand Down
3 changes: 2 additions & 1 deletion src/view/newtOkCancelMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ void Newt::okCancelMessage(const char* title, const char* message)
abort();
break;
default:
throw;
throw std::runtime_error(
"Something happened. Please run the software again");
}
}

Expand Down

0 comments on commit 2a2e501

Please sign in to comment.