Skip to content

Commit

Permalink
c-api: Forward specific SIL Kit exceptions (#146)
Browse files Browse the repository at this point in the history
* dev: Forward specific exception through the hourglass
  • Loading branch information
KonradBkd authored Dec 20, 2024
1 parent 0310872 commit 438f3e0
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 137 deletions.
35 changes: 26 additions & 9 deletions SilKit/include/silkit/capi/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,32 @@ typedef struct SilKit_Experimental_SystemController SilKit_Experimental_SystemCo

typedef int32_t SilKit_ReturnCode;

#define SilKit_ReturnCode_SUCCESS ((SilKit_ReturnCode)0)
#define SilKit_ReturnCode_UNSPECIFIEDERROR ((SilKit_ReturnCode)1)
#define SilKit_ReturnCode_NOTSUPPORTED ((SilKit_ReturnCode)2)
#define SilKit_ReturnCode_NOTIMPLEMENTED ((SilKit_ReturnCode)3)
#define SilKit_ReturnCode_BADPARAMETER ((SilKit_ReturnCode)4)
#define SilKit_ReturnCode_BUFFERTOOSMALL ((SilKit_ReturnCode)5)
#define SilKit_ReturnCode_TIMEOUT ((SilKit_ReturnCode)6)
#define SilKit_ReturnCode_UNSUPPORTEDSERVICE ((SilKit_ReturnCode)7)
#define SilKit_ReturnCode_WRONGSTATE ((SilKit_ReturnCode)8) // Returned on exception SilKit::StateError (CapiImpl.h)
#define SilKit_ReturnCode_SUCCESS ((SilKit_ReturnCode)0)
#define SilKit_ReturnCode_UNSPECIFIEDERROR ((SilKit_ReturnCode)1)
#define SilKit_ReturnCode_NOTSUPPORTED ((SilKit_ReturnCode)2)
#define SilKit_ReturnCode_NOTIMPLEMENTED ((SilKit_ReturnCode)3)
#define SilKit_ReturnCode_BADPARAMETER ((SilKit_ReturnCode)4)
#define SilKit_ReturnCode_BUFFERTOOSMALL ((SilKit_ReturnCode)5)
#define SilKit_ReturnCode_TIMEOUT ((SilKit_ReturnCode)6)
#define SilKit_ReturnCode_UNSUPPORTEDSERVICE ((SilKit_ReturnCode)7)

// The following return codes have an corresponding specific SIL Kit exception.
// If an error occurs and a specific exception is thrown, it is caught in
// CapiImpl.hpp and the code defined here is returned.
// This completes the error handling for the usage of the C-API.

// For the C++-Api, the return code is translated back to the specific SIL Kit
// exception and thrown in ThrowOnError.hpp.

#define SilKit_ReturnCode_WRONGSTATE ((SilKit_ReturnCode)8) // SilKit::StateError
#define SilKit_ReturnCode_TYPECONVERSIONERROR ((SilKit_ReturnCode)9) // SilKit::TypeConversionError
#define SilKit_ReturnCode_CONFIGURATIONERROR ((SilKit_ReturnCode)10) // SilKit::ConfigurationError
#define SilKit_ReturnCode_PROTOCOLERROR ((SilKit_ReturnCode)11) // SilKit::ProtocolError
#define SilKit_ReturnCode_ASSERTIONERROR ((SilKit_ReturnCode)12) // SilKit::AssertionError
#define SilKit_ReturnCode_EXTENSIONERROR ((SilKit_ReturnCode)13) // SilKit::ExtensionError
#define SilKit_ReturnCode_LOGICERROR ((SilKit_ReturnCode)14) // SilKit::LogicError
#define SilKit_ReturnCode_LENGTHERROR ((SilKit_ReturnCode)15) // SilKit::LengthError
#define SilKit_ReturnCode_OUTOFRANGEERROR ((SilKit_ReturnCode)16) // SilKit::OutOfRangeError

typedef uint64_t SilKit_NanosecondsTime;

Expand Down
24 changes: 23 additions & 1 deletion SilKit/include/silkit/detail/impl/ThrowOnError.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,29 @@ void ThrowOnError(SilKit_ReturnCode returnCode)
std::ostringstream os;
os << "SIL Kit: " << returnCodeCstr << " (" << returnCode << "): " << lastErrorCstr;

throw SilKitError{os.str()};
switch (returnCode)
{
case SilKit_ReturnCode_WRONGSTATE:
throw StateError{os.str()};
case SilKit_ReturnCode_TYPECONVERSIONERROR:
throw TypeConversionError{os.str()};
case SilKit_ReturnCode_CONFIGURATIONERROR:
throw ConfigurationError{os.str()};
case SilKit_ReturnCode_PROTOCOLERROR:
throw ProtocolError{os.str()};
case SilKit_ReturnCode_ASSERTIONERROR:
throw AssertionError{os.str()};
case SilKit_ReturnCode_EXTENSIONERROR:
throw ExtensionError{os.str()};
case SilKit_ReturnCode_LOGICERROR:
throw LogicError{os.str()};
case SilKit_ReturnCode_LENGTHERROR:
throw LengthError{os.str()};
case SilKit_ReturnCode_OUTOFRANGEERROR:
throw OutOfRangeError{os.str()};
default:
throw SilKitError{os.str()};
}
}
}

Expand Down
1 change: 1 addition & 0 deletions SilKit/source/capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ add_silkit_test_to_executable(SilKitUnitTests SOURCES Test_CapiTimeSync.cpp LIBS
add_silkit_test_to_executable(SilKitUnitTests SOURCES Test_CapiLin.cpp LIBS S_SilKitImpl I_SilKit_Core_Mock_Participant)
add_silkit_test_to_executable(SilKitUnitTests SOURCES Test_CapiSymbols.cpp LIBS S_SilKitImpl)
add_silkit_test_to_executable(SilKitUnitTests SOURCES Test_CapiNetSim.cpp LIBS S_SilKitImpl I_SilKit_Core_Mock_Participant)
add_silkit_test_to_executable(SilKitUnitTests SOURCES Test_CapiExceptions.cpp LIBS S_SilKitImpl)

40 changes: 40 additions & 0 deletions SilKit/source/capi/CapiImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,46 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_WRONGSTATE; \
} \
catch (const SilKit::TypeConversionError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_TYPECONVERSIONERROR; \
} \
catch (const SilKit::ConfigurationError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_CONFIGURATIONERROR; \
} \
catch (const SilKit::ProtocolError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_PROTOCOLERROR; \
} \
catch (const SilKit::AssertionError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_ASSERTIONERROR; \
} \
catch (const SilKit::ExtensionError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_EXTENSIONERROR; \
} \
catch (const SilKit::LengthError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_LENGTHERROR; \
} \
catch (const SilKit::OutOfRangeError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_OUTOFRANGEERROR; \
} \
catch (const SilKit::LogicError& e) \
{ \
SilKit_error_string = e.what(); \
return SilKit_ReturnCode_LOGICERROR; \
} \
catch (const SilKit::SilKitError& e) \
{ \
SilKit_error_string = e.what(); \
Expand Down
97 changes: 97 additions & 0 deletions SilKit/source/capi/Test_CapiExceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* Copyright (c) 2022 Vector Informatik GmbH
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "CapiImpl.hpp"
#include "silkit/detail/impl/ThrowOnError.hpp"

namespace {

class Test_CapiExceptions : public testing::Test
{
public:
Test_CapiExceptions() {}
};

class UnknownException : public std::exception
{
public:
UnknownException() {}
UnknownException(const char* /*message*/) {}
};

template <typename T>
SilKit_ReturnCode TestExceptionToErrorCode()
try
{
throw T{"error msg"};
}
CAPI_CATCH_EXCEPTIONS

// Not all compilers support std::exception with const char* initialization, so treat that std::exception separately
SilKit_ReturnCode TestStdExceptionToErrorCode()
try
{
throw std::exception();
}
CAPI_CATCH_EXCEPTIONS


// Each catch branch of CapiImpl.hpp CAPI_CATCH_EXCEPTIONS is tested for the expected return code
TEST_F(Test_CapiExceptions, catch_exception_macro)
{
EXPECT_EQ(TestExceptionToErrorCode<SilKit::CapiBadParameterError>(), SilKit_ReturnCode_BADPARAMETER);

EXPECT_EQ(TestExceptionToErrorCode<SilKit::TypeConversionError>(), SilKit_ReturnCode_TYPECONVERSIONERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::ConfigurationError>(), SilKit_ReturnCode_CONFIGURATIONERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::StateError>(), SilKit_ReturnCode_WRONGSTATE);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::ProtocolError>(), SilKit_ReturnCode_PROTOCOLERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::AssertionError>(), SilKit_ReturnCode_ASSERTIONERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::ExtensionError>(), SilKit_ReturnCode_EXTENSIONERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::LogicError>(), SilKit_ReturnCode_LOGICERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::LengthError>(), SilKit_ReturnCode_LENGTHERROR);
EXPECT_EQ(TestExceptionToErrorCode<SilKit::OutOfRangeError>(), SilKit_ReturnCode_OUTOFRANGEERROR);

EXPECT_EQ(TestExceptionToErrorCode<SilKit::SilKitError>(), SilKit_ReturnCode_UNSPECIFIEDERROR);
EXPECT_EQ(TestExceptionToErrorCode<std::runtime_error>(), SilKit_ReturnCode_UNSPECIFIEDERROR);
EXPECT_EQ(TestExceptionToErrorCode<UnknownException>(), SilKit_ReturnCode_UNSPECIFIEDERROR);
EXPECT_EQ(TestStdExceptionToErrorCode(), SilKit_ReturnCode_UNSPECIFIEDERROR);
}

// Test that the C-API return code results in the correct exception
TEST_F(Test_CapiExceptions, throw_on_error)
{
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_TYPECONVERSIONERROR),
SilKit::TypeConversionError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_CONFIGURATIONERROR), SilKit::ConfigurationError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_WRONGSTATE), SilKit::StateError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_PROTOCOLERROR), SilKit::ProtocolError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_ASSERTIONERROR), SilKit::AssertionError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_EXTENSIONERROR), SilKit::ExtensionError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_LOGICERROR), SilKit::LogicError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_LENGTHERROR), SilKit::LengthError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_OUTOFRANGEERROR), SilKit::OutOfRangeError);
EXPECT_THROW(SilKit::_detail_v1::Impl::ThrowOnError(SilKit_ReturnCode_UNSPECIFIEDERROR), SilKit::SilKitError);
}


} // namespace
Loading

0 comments on commit 438f3e0

Please sign in to comment.