Skip to content

Commit

Permalink
Merge 34e196b into de8b30d
Browse files Browse the repository at this point in the history
  • Loading branch information
azeey authored Jan 18, 2023
2 parents de8b30d + 34e196b commit 1ae23b3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
21 changes: 14 additions & 7 deletions include/sdf/Error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ namespace sdf
/// \sa Element::SetXmlPath
public: void SetXmlPath(const std::string &_xmlPath);

/// \brief It will print the error to _out or throw it using
/// SDF_ASSERT depending on its ErrorCode.
/// \param[in] _out ostream to use for printing errors.
public: void ThrowOrPrintError(sdf::Console::ConsoleStream &_out) const;

/// \brief Safe bool conversion.
/// \return True if this Error's Code() != NONE. In otherwords, this is
/// true when there is an error.
Expand All @@ -277,8 +272,20 @@ namespace sdf
/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
}
}

/// \brief Internal namespace. Functions and classes defined in this namespace
/// are for internal use only and maybe removed without a deprecation cycle.
namespace internal
{
/// \brief Prints the error to _out or throw using SDF_ASSERT depending on the
/// ErrorCode in _error.
/// \param[out] _out ostream to use for printing errors.
/// \param[in] _error _error The error object to be printed
void SDFORMAT_VISIBLE throwOrPrintError(sdf::Console::ConsoleStream &_out,
const sdf::Error &_error);
} // namespace internal
} // namespace SDF_VERSION_NAMESPACE
} // namespace sdf
#ifdef _WIN32
#pragma warning(pop)
#endif
Expand Down
32 changes: 18 additions & 14 deletions src/Error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@ void Error::SetXmlPath(const std::string &_xmlPath)
this->dataPtr->xmlPath = _xmlPath;
}

/////////////////////////////////////////////////
void Error::ThrowOrPrintError(sdf::Console::ConsoleStream &_out) const
{
if (this->dataPtr->code == sdf::ErrorCode::FATAL_ERROR)
{
SDF_ASSERT(false, this->dataPtr->message);
}
else
{
_out << this->dataPtr->message;
}
}

/////////////////////////////////////////////////
Error::operator bool() const
{
Expand Down Expand Up @@ -174,5 +161,22 @@ std::ostream &operator<<(std::ostream &_out, const sdf::Error &_err)
<< "Msg: " << _err.Message();
return _out;
}

namespace internal
{

void throwOrPrintError(sdf::Console::ConsoleStream &_out,
const sdf::Error &_error)
{
if (_error.Code() == sdf::ErrorCode::FATAL_ERROR)
{
SDF_ASSERT(false, _error.Message());
}
else
{
_out << _error.Message();
}
}
}
} // namespace internal
} // namespace SDF_VERSION_NAMESPACE
} // namespace sdf
33 changes: 33 additions & 0 deletions src/Error_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include <gtest/gtest.h>
#include <optional>
#include "sdf/sdf_config.h"
#include "sdf/Exception.hh"
#include "sdf/Error.hh"
#include "test_utils.hh"

/////////////////////////////////////////////////
TEST(Error, DefaultConstruction)
Expand Down Expand Up @@ -134,3 +136,34 @@ TEST(Error, ValueConstructionWithXmlPath)
FAIL();
}

/////////////////////////////////////////////////
TEST(Error, ThrowOrPrint)
{
#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(false);
sdf::testing::ScopeExit revertSetQuiet(
[]
{
sdf::Console::Instance()->SetQuiet(true);
});
#endif
{
sdf::Error error(sdf::ErrorCode::DUPLICATE_NAME, "Duplicate found");
std::stringstream buffer;
sdf::testing::RedirectConsoleStream redir(
sdf::Console::Instance()->GetMsgStream(), &buffer);

sdf::internal::throwOrPrintError(sdferr, error);
EXPECT_NE(std::string::npos, buffer.str().find("Duplicate found"))
<< buffer.str();
}

{
std::stringstream buffer;
sdf::testing::RedirectConsoleStream redir(
sdf::Console::Instance()->GetMsgStream(), &buffer);
sdf::Error error(sdf::ErrorCode::FATAL_ERROR, "Fatal Error");
EXPECT_THROW(sdf::internal::throwOrPrintError(sdferr, error),
sdf::AssertionInternalError);
}
}
1 change: 1 addition & 0 deletions src/Exception.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Exception::Exception(const char *_file, std::int64_t _line, std::string _msg)
this->dataPtr->file = _file;
this->dataPtr->line = _line;
this->dataPtr->str = _msg;
// TODO(azeey) Remove Print for libsdformat 14.0
this->Print();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void throwOrPrintErrors(const sdf::Errors& _errors)
{
for(auto& error : _errors)
{
error.ThrowOrPrintError(sdferr);
internal::throwOrPrintError(sdferr, error);
}
}

Expand Down

0 comments on commit 1ae23b3

Please sign in to comment.