Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1032 Improve output our error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Feb 4, 2024
1 parent bf2d793 commit 49eb12d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

// additional includes
#include "iox/error_reporting/types.hpp"
#include "iox/log/logstream.hpp"

namespace iox
{
Expand Down Expand Up @@ -62,6 +63,12 @@ enum class CBindingError : iox::er::ErrorCode::type

const char* asStringLiteral(const CBindingError error) noexcept;

inline log::LogStream& operator<<(log::LogStream& stream, CBindingError value) noexcept
{
stream << asStringLiteral(value);
return stream;
}

class CBindingErrorType
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

// additional includes
#include "iox/error_reporting/types.hpp"
#include "iox/log/logstream.hpp"

namespace iox
{
Expand All @@ -51,6 +52,12 @@ enum class HoofsError : iox::er::ErrorCode::type

const char* asStringLiteral(const HoofsError error) noexcept;

inline log::LogStream& operator<<(log::LogStream& stream, HoofsError value) noexcept
{
stream << asStringLiteral(value);
return stream;
}

class HoofsErrorType
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace er
// Custom panic with location
[[noreturn]] inline void panic(const SourceLocation& location)
{
IOX_ERROR_INTERNAL_LOG_PANIC(location, "Panic");
IOX_ERROR_INTERNAL_LOG_PANIC(location, "[PANIC]");
panic();
}

Expand All @@ -64,7 +64,7 @@ namespace er
template <class Message>
[[noreturn]] inline void panic(const SourceLocation& location, Message&& msg)
{
IOX_ERROR_INTERNAL_LOG_PANIC(location, "Panic " << msg);
IOX_ERROR_INTERNAL_LOG_PANIC(location, "[PANIC] " << msg);
panic();
}

Expand All @@ -80,8 +80,8 @@ inline void report(const SourceLocation& location, Kind, const Error& error)
auto errorName = toErrorName(error);

IOX_ERROR_INTERNAL_LOG(location,
"'" << errorName << "' (code " << code.value << ") in module '" << moduleName << "' (id "
<< module.value << ")");
"[" << errorName << " (code = " << code.value << ")] in module [" << moduleName
<< " (id = " << module.value << ")]");
auto& h = ErrorHandler::get();
h.onReportError(ErrorDescriptor(location, code, module));
}
Expand All @@ -100,8 +100,8 @@ inline void report(const SourceLocation& location, iox::er::FatalKind kind, cons
auto errorName = toErrorName(error);

IOX_ERROR_INTERNAL_LOG_FATAL(location,
"'" << kind.name << "' '" << errorName << "' (code " << code.value << ") in module '"
<< moduleName << "' (id " << module.value << ")");
"[" << kind.name << "] [" << errorName << " (code = " << code.value << ")] in module ["
<< moduleName << " (id = " << module.value << ")]");
auto& h = ErrorHandler::get();
h.onReportError(ErrorDescriptor(location, code, module));
}
Expand All @@ -119,11 +119,11 @@ inline void report(const SourceLocation& location, Kind kind, const Error& error
auto module = toModule(error);
if constexpr (std::is_same<NoMessage, Message>::value)
{
IOX_ERROR_INTERNAL_LOG_FATAL(location, kind.name);
IOX_ERROR_INTERNAL_LOG_FATAL(location, "[" << kind.name << "]");
}
else
{
IOX_ERROR_INTERNAL_LOG_FATAL(location, kind.name << " " << std::forward<Message>(msg));
IOX_ERROR_INTERNAL_LOG_FATAL(location, "[" << kind.name << "] " << std::forward<Message>(msg));
}
auto& h = ErrorHandler::get();
h.onReportViolation(ErrorDescriptor(location, code, module));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct AssertViolationKind

struct EnforceViolationKind
{
static constexpr char const* name = "EnforceViolation";
static constexpr char const* name = "Enforce Violation";
};

template <class T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
location.line, \
location.function, \
iox::log::LogLevel::ERROR, \
location.file << " line " << location.line << ": " << msg_stream)
location.file << ":" << location.line << " " << msg_stream)

/// @brief Log the location of a fatal error.
/// @param location the location of the error
Expand All @@ -41,7 +41,7 @@
location.line, \
location.function, \
iox::log::LogLevel::FATAL, \
location.file << " line " << location.line << ": " << msg_stream)
location.file << ":" << location.line << " " << msg_stream)

/// @brief Log a panic invocation.
/// @param location the location of the panic invocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
#ifndef IOX_HOOFS_TESTING_FATAL_FAILURE_HPP
#define IOX_HOOFS_TESTING_FATAL_FAILURE_HPP

#include "iox/error_reporting/error_kind.hpp"
#include "iox/error_reporting/types.hpp"
#include "iox/function_ref.hpp"
#include "iox/optional.hpp"
#include "iox/logging.hpp"

#include "iceoryx_hoofs/testing/error_reporting/testing_support.hpp"
#include "test.hpp"

#include <atomic>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,54 @@
#ifndef IOX_HOOFS_TESTING_FATAL_FAILURE_INL
#define IOX_HOOFS_TESTING_FATAL_FAILURE_INL

#include "iceoryx_hoofs/testing/error_reporting/testing_support.hpp"
#include "iceoryx_hoofs/testing/fatal_failure.hpp"

namespace iox
{
namespace testing
{
template <typename ErrorType>
// NOLINTJUSTIFICATION The complexity comes from the expanded macros; without the expansions the functions is quite readable
// NOLINTNEXTLINE(readability-function-size, readability-function-cognitive-complexity)
inline bool IOX_EXPECT_FATAL_FAILURE(const function_ref<void()> testFunction,
const ErrorType expectedError [[maybe_unused]])
{
iox::testing::ErrorHandler::instance().reset();
runInTestThread([&] { testFunction(); });
IOX_TESTING_EXPECT_PANIC();

auto hasExpectedError{false};
if constexpr (std::is_same_v<ErrorType, iox::er::FatalKind>)
{
hasExpectedError = iox::testing::hasPanicked();
if (!hasExpectedError)
{
IOX_LOG(ERROR, "Expected '" << iox::er::FatalKind::name << "' but it did not happen!");
}
}
else if constexpr (std::is_same_v<ErrorType, iox::er::EnforceViolationKind>)
{
hasExpectedError = iox::testing::hasEnforceViolation() && iox::testing::hasPanicked();
if (!hasExpectedError)
{
IOX_LOG(ERROR, "Expected '" << iox::er::EnforceViolationKind::name << "' but it did not happen!");
}
}
else if constexpr (std::is_same_v<ErrorType, iox::er::AssertViolationKind>)
{
hasExpectedError = iox::testing::hasAssertViolation() && iox::testing::hasPanicked();
if (!hasExpectedError)
{
IOX_LOG(ERROR, "Expected '" << iox::er::AssertViolationKind::name << "' but it did not happen!");
}
}
else
{
hasExpectedError = iox::testing::hasError(expectedError) && iox::testing::hasPanicked();
if (!hasExpectedError)
{
IOX_LOG(ERROR, "Expected an '" << expectedError << "' error but it did not happen!");
}
}

EXPECT_TRUE(hasExpectedError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

// additional includes
#include "iox/error_reporting/types.hpp"
#include "iox/log/logstream.hpp"

namespace iox
{
Expand Down Expand Up @@ -182,6 +183,12 @@ enum class PoshError : iox::er::ErrorCode::type

const char* asStringLiteral(const PoshError error) noexcept;

inline log::LogStream& operator<<(log::LogStream& stream, PoshError value) noexcept
{
stream << asStringLiteral(value);
return stream;
}

class PoshErrorType
{
public:
Expand Down

0 comments on commit 49eb12d

Please sign in to comment.