Skip to content

Commit

Permalink
libguard: Adding user defined exceptions
Browse files Browse the repository at this point in the history
Changes:
-Adding user defined exceptions in libguard so that the caller
can distinguish the type of error at his end.
-Changes in respective UTs.

Test:
Created guard record, tried creating the same.
Deleting a non guarded record.
Ran UTs in docker env.

Signed-off-by: Chirag Sharma <[email protected]>
  • Loading branch information
Chirag Sharma committed Aug 17, 2021
1 parent 8965dbb commit e23e7df
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 51 deletions.
20 changes: 12 additions & 8 deletions libguard/guard_common.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include "guard_exception.hpp"
#include "guard_log.hpp"

#include <array>
#include <cstdint>
#include <stdexcept>
#include <optional>
#include <stdexcept>

namespace openpower
{
namespace guard
{

using namespace openpower::guard::exception;

/* From hostboot: src/include/usr/targeting/common/entitypath.H */
struct EntityPath
{
Expand Down Expand Up @@ -75,15 +79,15 @@ struct EntityPath
GUARD_ERROR,
"Size mismatch. Given buf size[%d] EntityPath sizeof[%d]",
rawData.size(), sizeof(EntityPath));
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

if (rawData.size() == 0)
{
openpower::guard::log::guard_log(GUARD_ERROR,
"Given raw data is empty");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -93,7 +97,7 @@ struct EntityPath
{
openpower::guard::log::guard_log(
GUARD_ERROR, "PathElement size mismatch in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -107,7 +111,7 @@ struct EntityPath
openpower::guard::log::guard_log(
GUARD_ERROR,
"Insufficient data for PathElement in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -122,7 +126,7 @@ struct EntityPath
{
openpower::guard::log::guard_log(GUARD_ERROR,
"Given raw data is empty");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath conversion constructor failed");
}

Expand All @@ -135,7 +139,7 @@ struct EntityPath
GUARD_ERROR,
"Size mismatch. Given path elements size[%d] max[%d]",
pathElementsSize, maxPathElements);
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath conversion constructor failed");
}

Expand All @@ -147,7 +151,7 @@ struct EntityPath
openpower::guard::log::guard_log(
GUARD_ERROR,
"Insufficient data for PathElement in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath conversion constructor failed");
}

Expand Down
141 changes: 141 additions & 0 deletions libguard/guard_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#pragma once

#include <exception>
#include <string>

namespace openpower
{
namespace guard
{
namespace exception
{
class GuardFileOpenFailed : public std::exception
{
public:
explicit GuardFileOpenFailed(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class GuardFileReadFailed : public std::exception
{
public:
explicit GuardFileReadFailed(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class GuardFileWriteFailed : public std::exception
{
public:
explicit GuardFileWriteFailed(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class GuardFileSeekFailed : public std::exception
{
public:
explicit GuardFileSeekFailed(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class InvalidEntityPath : public std::exception
{
public:
explicit InvalidEntityPath(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class InvalidEntry : public std::exception
{
public:
explicit InvalidEntry(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class AlreadyGuarded : public std::exception
{
public:
explicit AlreadyGuarded(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class GuardFileOverFlowed : public std::exception
{
public:
explicit GuardFileOverFlowed(const std::string& message) : message(message)
{
}

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};
} // namespace exception
} // namespace guard
} // namespace openpower
43 changes: 26 additions & 17 deletions libguard/guard_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "guard_file.hpp"

#include "guard_exception.hpp"
#include "guard_log.hpp"

#include <cstring>
Expand All @@ -12,6 +13,7 @@ namespace openpower
namespace guard
{
using namespace openpower::guard::log;
using namespace openpower::guard::exception;

GuardFile::GuardFile(const fs::path& file) : guardFile(file)
{
Expand All @@ -22,22 +24,27 @@ GuardFile::GuardFile(const fs::path& file) : guardFile(file)
{
guard_log(GUARD_ERROR,
"Failed to open the GUARD file during initailization");
throw std::runtime_error("Failed to get the GUARD file.");
throw GuardFileOpenFailed(
"Exception thrown as failed to open the guard file");
}
file.seekg(0, file.end);
if (file.fail())
{
throw std::runtime_error(
"Failed to move to the last position in the file");
guard_log(GUARD_ERROR,
"Failed to move to last position in guard file");
throw GuardFileSeekFailed(
"Exception thrown as failed to move to the "
"last position in the file");
}
fileSize = file.tellg();
}
catch (const std::exception& ex)
catch (openpower::guard::exception::GuardFileOpenFailed& ex)
{
guard_log(GUARD_ERROR,
"Error caught during initializing the GUARD file %s", ex.what());
throw std::runtime_error(
"Exception thrown during initialize GUARD file.");
throw GuardFileOpenFailed(ex.what());
}
catch (openpower::guard::exception::GuardFileSeekFailed& ex)
{
throw GuardFileSeekFailed(ex.what());
}
}

Expand All @@ -48,7 +55,8 @@ void GuardFile::read(const uint64_t pos, void* dst, const uint64_t len)
{
guard_log(GUARD_ERROR,
"Unable to open the guard file during read operation.");
throw std::runtime_error("Failed to open guard file in read function.");
throw GuardFileOpenFailed(
"Failed to open guard file in read function.");
}
file.seekg(pos, file.beg);
if (file.fail())
Expand All @@ -57,14 +65,15 @@ void GuardFile::read(const uint64_t pos, void* dst, const uint64_t len)
"Unable to move to the position in the guard file at"
" position= 0x%016llx",
pos);
throw std::runtime_error("Failed to move to the position in the file");
throw GuardFileSeekFailed("Failed to move to the position during read "
"operation in the guard file");
}
file.read(reinterpret_cast<char*>(dst), len);
if (file.fail())
{
guard_log(GUARD_ERROR,
"Unable to read from guard file at position= 0x%016llx", pos);
throw std::runtime_error("Failed to read from guard file.");
throw GuardFileReadFailed("Failed to read from guard file.");
}
return;
}
Expand All @@ -78,7 +87,7 @@ void GuardFile::write(const uint64_t pos, const void* src, const uint64_t len)
guard_log(
GUARD_ERROR,
"Unable to open guard file while perfoming the write operation");
throw std::runtime_error("Failed to open guard file to write");
throw GuardFileOpenFailed("Failed to open guard file to write");
}

file.seekp(pos, file.beg);
Expand All @@ -88,15 +97,15 @@ void GuardFile::write(const uint64_t pos, const void* src, const uint64_t len)
"Unable to move to the position in the guard file."
" Position= 0x%016llx",
pos);
throw std::runtime_error("Failed to move to the position in the guard file.");
throw GuardFileSeekFailed("Failed to move to the position during write "
"operation in the guard file.");
}

file.write(reinterpret_cast<const char*>(src), len);
if (file.fail())
{
guard_log(GUARD_ERROR,
"Unable to write the record to GUARD file.");
throw std::runtime_error("Failed to write to the guard file.");
guard_log(GUARD_ERROR, "Unable to write the record to GUARD file.");
throw GuardFileWriteFailed("Failed to write to the guard file.");
}
return;
}
Expand All @@ -109,7 +118,7 @@ void GuardFile::erase(const uint64_t pos, const uint64_t len)
if (len <= 0)
{
guard_log(GUARD_ERROR, "Length passed is %d which is not valid", len);
throw std::runtime_error("Not a valid length value");
throw InvalidEntry("Not a valid length value");
}

while (len - rlen > 0)
Expand Down
Loading

0 comments on commit e23e7df

Please sign in to comment.