Skip to content

Commit

Permalink
exception: Add .get_errno() method
Browse files Browse the repository at this point in the history
Add the .get_errno() method to provide the errno value from SdBusError.

Closes #22

Tested: Added check for the new method to the exception
unit test suite. Also verified external callers get the right
errno value with:
    catch (const SdBusError& e)
    {
        int errno = e.get_errno());
    }

Change-Id: Idddfa7f1bd9cfabfaf89e4d6c83146b4b9af211f
Signed-off-by: Adriana Kobylak <[email protected]>
  • Loading branch information
anoo1 committed Sep 21, 2018
1 parent 5b701c6 commit 33335b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sdbusplus/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ const char* SdBusError::what() const noexcept
return full_message.c_str();
}

int SdBusError::get_errno() const noexcept
{
return intf->sd_bus_error_get_errno(&this->error);
}

void SdBusError::populateMessage(const char* prefix)
{
full_message = prefix;
Expand Down
1 change: 1 addition & 0 deletions sdbusplus/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SdBusError final : public internal_exception
const char* name() const noexcept override;
const char* description() const noexcept override;
const char* what() const noexcept override;
int get_errno() const noexcept;

private:
sd_bus_error error;
Expand Down
9 changes: 9 additions & 0 deletions test/exception/sdbus_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ TEST(SdBusError, BasicErrno)

// Make sure inheritance is defined correctly
sdbusplus::exception::exception& sdbusErr = err;
SdBusError& errNew = err;
EXPECT_EQ(errorVal, errNew.get_errno());
EXPECT_EQ(std::string{error.name}, sdbusErr.name());
EXPECT_EQ(std::string{error.message}, sdbusErr.description());
std::exception& stdErr = sdbusErr;
Expand Down Expand Up @@ -80,6 +82,7 @@ TEST(SdBusError, Move)
// Build our first SdBusError
SdBusError err(errorVal, prefix.c_str());

EXPECT_EQ(errorVal, err.get_errno());
EXPECT_EQ(name, err.name());
EXPECT_EQ(message, err.description());
EXPECT_EQ(what, err.what());
Expand All @@ -88,11 +91,13 @@ TEST(SdBusError, Move)
SdBusError errNew(std::move(err));

// Ensure the old object was cleaned up
EXPECT_EQ(0, err.get_errno());
EXPECT_EQ(nullptr, err.name());
EXPECT_EQ(nullptr, err.description());
EXPECT_EQ(std::string{}, err.what());

// Ensure our new object has the same data but moved
EXPECT_EQ(errorVal, errNew.get_errno());
EXPECT_EQ(name, errNew.name());
EXPECT_EQ(message, errNew.description());
EXPECT_EQ(what, errNew.what());
Expand All @@ -101,12 +106,14 @@ TEST(SdBusError, Move)
errFinal = std::move(errNew);

// Ensure the old object was cleaned up
EXPECT_EQ(0, errNew.get_errno());
EXPECT_EQ(nullptr, errNew.name());
EXPECT_EQ(nullptr, errNew.description());
EXPECT_EQ(std::string{}, errNew.what());
}

// Ensure our new object has the same data but moved
EXPECT_EQ(errorVal, errFinal.get_errno());
EXPECT_EQ(name, errFinal.name());
EXPECT_EQ(message, errFinal.description());
EXPECT_EQ(what, errFinal.what());
Expand All @@ -124,6 +131,7 @@ TEST(SdBusError, BasicError)
sd_bus_error_set(&error, name.c_str(), description.c_str());
EXPECT_TRUE(sd_bus_error_is_set(&error));
const char* nameBeforeMove = error.name;
const int errorVal = sd_bus_error_get_errno(&error);
SdBusError err(&error, prefix.c_str());

// We expect a move not copy
Expand All @@ -134,6 +142,7 @@ TEST(SdBusError, BasicError)
sd_bus_error_free(&error);
sd_bus_error_free(&error);

EXPECT_EQ(errorVal, err.get_errno());
EXPECT_EQ(name, err.name());
EXPECT_EQ(description, err.description());
EXPECT_EQ(prefix + ": " + name + ": " + description, err.what());
Expand Down

0 comments on commit 33335b3

Please sign in to comment.